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

Craeg K Strong cstrong at
Fri Sep 27 17:23:30 CEST 2002

Thanks to all who responded!

I should have known to check the python cookbook...

One question: the sys._getframe() method is new for Python 2.1--
how likely do you think it is to change in 2.2+
releases?   The part that worries me is that the
method starts with an underscore.  That generally means
"protected/private" or "unpublished" to me.

I just want to know how safe it is to use for
general business application type programming.



Alex Martelli wrote:
> Craeg K Strong wrote:
>>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
>>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).
> Alex

More information about the Python-list mailing list