How can I know the name of "caller"
zacherates
zacherates at gmail.com
Tue Jun 19 16:56:08 EDT 2007
> ...(if it is possible) how can I get, from method "called", the name
> of function/method that called it (in this case "caller")?
The traceback module will give you access to the call stack.
>>> import traceback
>>> def foo():
... return traceback.extract_stack()
...
>>> def bar():
... return foo()
...
>>> foo()
[('<stdin>', 1, '<module>', None), ('<stdin>', 2, 'foo', None)]
>>> bar()
[('<stdin>', 1, '<module>', None), ('<stdin>', 2, 'bar', None),
('<stdin>', 2, 'foo', None)]
However, this seems like one of those it-matters-where-you-call-it-
from functions from days of yore. They're generally considered to be
a bad idea and was one of the patterns that prompted a rather famous
essay by Dijkstra.
I'd recommend exploring other design alternatives and leave this
monstrosity in peace.
Cheers,
Aaron
More information about the Python-list
mailing list