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

Alex Martelli aleax at
Mon Sep 30 11:19:31 EDT 2002

Craeg K Strong wrote:

> 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.

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).


More information about the Python-list mailing list