Refer to function name (newbie ques)
Pete Shinners
pete at shinners.org
Thu Jul 25 11:35:39 EDT 2002
chris ciotti wrote:
> Is there a way to get the name of the running function? In a script
> I'm doing some error checking and I want to be able to name the
> function that screws up. Something like:
one problem is if the function has multiple names...
def foo():
print 'In Foo'
bar = foo
bar()
of course, this if very uncommon. i think the only way to get at this info
would be to dig into a stack trace. this may end up being more helpful
anyways, because you can also print out who has called your function.
import traceback
def foo():
stack = traceback.extract_stack()
print 'Inside Function:', stack[-1][2]
print ' Called By:', stack[-2][2], ' line:', stack[-2][1]
the traceback module was added sometime around python-2.1 (i think?) so
this won't work with older pythons. docs are here,
http://www.python.org/doc/current/lib/module-traceback.html
http://www.python.org/doc/current/lib/traceback-example.html
More information about the Python-list
mailing list