Identifing current function

Peter Otten __peter__ at web.de
Wed Jan 21 22:44:51 EST 2004


Brian Donovan wrote:

>   Is there a way to identify the current function. For example:

>>> import inspect
>>> def whoami(): return inspect.stack()[1][3]
...
>>> class A:
...     def someMethod(self): print whoami()
...     def anotherMethod(self): print whoami()
...
>>> def someFunc():
...     print whoami()
...     a = A()
...     a.someMethod()
...     a.anotherMethod()
...
>>> someFunc()
someFunc
someMethod
anotherMethod
>>>

Seems to work. No warranty, though.

Peter




More information about the Python-list mailing list