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

Craeg K Strong cstrong at
Mon Sep 30 11:38:09 EDT 2002

Alex Martelli wrote:
> Craeg K Strong wrote:
>>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.
> Yes, the name convention does indicate "internal".
>>I just want to know how safe it is to use for
>>general business application type programming.
> Not very: it's intended to be used for debugging and
> development purposes.  If you can cleanly express your
> application's intent WITHOUT a function needing to know
> its own name, it's by far preferable to do so.  If such
> self-knowledge is indispensable to cleanly express your
> application's intent, then sys._getframe is as usable as
> any other method -- worst case, should it go away one
> day, you'll have to install it in your own startup code
> (code to emulate sys._getframe for old Python versions is
> also available in the Cookbook, and most likely similar
> code can be cooked up in the future, as long as there's
> _any_ way to get such to-you-indispensable information).
> Alex

Fair enough.  The functionality is not indespensible, but
rather something we would like to use for debugging purposes.

As such, I feel comfortable using it in limited, well
defined circumstances even though it is
"unpublished" functionality, as long as we
put in a big fat comment.
That way, when/if it breaks, we know what to do...

Thanks again,


More information about the Python-list mailing list