Is there a way to find the name of the method currently being executed?

Alex Martelli aleax at
Thu Sep 26 09:36:16 EDT 2002

Craeg K Strong wrote:

> Hello:
> class a:
>     def func1(self):
>         print "func1 being called"
>     def func2(self):
>         print "func2 being called"
> In the code sample above, I would like to replace the "func1"
> and "func2" strings with constants so the same code could
> be copied verbatim into multiple functions and do the right
> thing.
> Is there a way to do this in Python?  For example, maybe you could
> get your hands on the call stack and print out stack[0].__name__
> or something....

Right, that's exactly what I did in:

except that sys._getframe (gets one frame of the call stack,
not all of the stack at once) is what the recipe uses (as
usual, the version in the printed Cookbook, published by
O'Reilly, has better discussion, &c).


More information about the Python-list mailing list