With the python module
inspect, one can inspect (not kidding) the
run-time python stack. Among other things, this makes it possible to
get the name of the current function or callers.
Handy for logging or debugging purposes. A simple script to illustrate:
import inspect # functions def whoami(): return inspect.stack() def whosdaddy(): return inspect.stack() def foo(): print "hello, I'm %s, daddy is %s" % (whoami(), whosdaddy()) bar() def bar(): print "hello, I'm %s, daddy is %s" % (whoami(), whosdaddy()) johny = bar # call them! foo() bar() johny()
hello, I'm foo, daddy is ? hello, I'm bar, daddy is foo hello, I'm bar, daddy is ? hello, I'm bar, daddy is ?
How does it work?
inspect.stack()returns a list with frame records.
- In function
inspect.stack()is the frame record of the function that calls
- The fourth element of the frame record (
inspect.stack()) is the function name.
The rest should be obvious (check the python docs on inspect for more information), except that
johny() thinks he's function
bar(). That's because
johny actually just points to the code of
bar() and the function name
bar is associated with the code and not with the variables.