Return name of caller function?
For example, how do I get this to work? def func(): print "This is", __?__ return __caller__ def echo(): print "This is ", __?__ return func() >>> print echo() This is echo This is func echo Yahoo! oneSearch: Finally, mobile search that gives answers, not web links. http://mobile.yahoo.com/mobileweb/onesearch?refer=1ONXIC -- http://mail.python.org/mailman/listinfo/python-list
Re: Return name of caller function?
--- Stephen R Laniel <[EMAIL PROTECTED]> wrote: > On Mon, Jun 25, 2007 at 06:27:29PM -0700, Matthew Peter wrote: > > For example, how do I get this to work? > > > > def func(): > > print "This is", __?__ > > return __caller__ > > > > def echo(): > > print "This is ", __?__ > > return func() > > inspect is your friend: > http://docs.python.org/lib/inspect-stack.html > > -- > Stephen R. Laniel > [EMAIL PROTECTED] > Cell: +(617) 308-5571 > http://laniels.org/ > PGP key: http://laniels.org/slaniel.key > -- > http://mail.python.org/mailman/listinfo/python-list > --- Stephen R Laniel <[EMAIL PROTECTED]> wrote: > On Mon, Jun 25, 2007 at 06:27:29PM -0700, Matthew Peter wrote: > > For example, how do I get this to work? > > > > def func(): > > print "This is", __?__ > > return __caller__ > > > > def echo(): > > print "This is ", __?__ > > return func() > > inspect is your friend: > http://docs.python.org/lib/inspect-stack.html > > -- > Stephen R. Laniel > [EMAIL PROTECTED] > Cell: +(617) 308-5571 > http://laniels.org/ > PGP key: http://laniels.org/slaniel.key > -- > http://mail.python.org/mailman/listinfo/python-list > Parsing the stack's tuple to get those attributes didn't feel reliable or pythonic. I am likely overlooking something. Is there a brief example you could show me in the context of using inspect to accomplish the goal I outlined above? The goal is using a function and not a class. Thanks! Yahoo! oneSearch: Finally, mobile search that gives answers, not web links. http://mobile.yahoo.com/mobileweb/onesearch?refer=1ONXIC -- http://mail.python.org/mailman/listinfo/python-list
Re: Return name of caller function?
--- Jay Loden <[EMAIL PROTECTED]> wrote:
>
> Matthew Peter wrote:
> > For example, how do I get this to work?
> >
> > def func():
> > print "This is", __?__
> > return __caller__
> >
> > def echo():
> > print "This is ", __?__
> > return func()
> >
> >
> >>>> print echo()
> > This is echo
> > This is func
> > echo
>
> This may not be what you're looking for but here's the solution I ended up
> with
> after some help from the list. It's designed for getting the name of an
> instance
> method, but in case it applies to your particular situation:
>
> #!/usr/bin/python
>
> import functools
>
> class TestClass:
> def __init__(self):
> pass
>
> def __getattr__(self, name):
> try:
> return getattr(self.__class__, name)
> except AttributeError:
> return functools.partial(self.foo, name)
>
> def foo(self, name, **args):
> print "This is", name
>
> test = TestClass()
> test.someMethod()
> test.anotherMethod()
>
> Otherwise the inspect module may be the way to go, as Stephen already pointed
> out
> (though I must admit it seems a very inelegant route, especially compared to
> Python's usually clean and clear style).
>
> -Jay
>
Thanks for the reply.
The goal is to get it working outside of a class. Overloading the method with
getattr semi-works but it doesn't work in the larger context of what I am
trying to
accomplish.
You could avoid the exception block in your example:
> def __getattr__(self, name):
> try:
> return getattr(self.__class__, name)
> except AttributeError:
> return functools.partial(self.foo, name)
to:
def __getattr__(self, name, *args, **kw):
return getattr(self.__class__, name, functools.partial(self.foo, name,
*args,
**kw))
def foo(self, name, *args, **kw):
print "This is", name, args, kw
print test.squeaky("I need oil")
Looking for a deal? Find great prices on flights and hotels with Yahoo!
FareChase.
http://farechase.yahoo.com/
--
http://mail.python.org/mailman/listinfo/python-list
log caller
Is it possible to print the function calls to a module? Like: test.py import mymod print mymod.x() mymod.py # each time a function is called we print out the called function and module print 'Func call: %s from %s' % (???, ???) def x(): return 'hello' Where would I pick up the ??? variables? A brief example would be nice too :) Thanks in advance! The fish are biting. Get more visitors on your site using Yahoo! Search Marketing. http://searchmarketing.yahoo.com/arp/sponsoredsearch_v2.php -- http://mail.python.org/mailman/listinfo/python-list
Re: Return name of caller function?
> The code below doesn't do the trick for you? > > #!/usr/bin/python > import inspect > > def master(): > print "I am the master" > slave() > > def slave(): > stack = inspect.stack() > caller = stack[1][3] > print "I am the slave; my caller was %s" % caller > > def main(): > master() > > if __name__ == '__main__': > main() [..] Yes. That does work. I was testing with an aliased instance of function which was returning >>>a = slave >>>print a() Is regex'ing stack[1][4] the only way to return 'a' in this instance? Be a PS3 game guru. Get your game face on with the latest PS3 news and previews at Yahoo! Games. http://videogames.yahoo.com/platform?platform=120121 -- http://mail.python.org/mailman/listinfo/python-list
