how can I get the name of a method inside it?
Alex Martelli
aleax at mac.com
Sun Jun 3 15:47:56 EDT 2007
"Sébastien Vincent" <sebastien_nimp73<@> wrote:
> I would like to know if it's possible to retrieve the name of a method when
> you're inside it. For example, in the following script, I would like to
> assign _s so that it prints "you are in method1".
>
>
> ***************************************
> class Obj1:
> def __init__(self):
> ...
>
> def method1(self):
> _s = ???
> print "you are in %s" % _s
For debugging purposes,
_s = sys._getframe(0).f_code.co_name
should work (after you've done an "import sys" somewhere appropriate, of
course;-). The _ in front of _getframe, as well as the klunkiness of it
all, are all indications that this is _not_ recommended for "production
use" -- like most of Python's introspection features, it IS chiefly
meant for debugging purposes.
Alex
More information about the Python-list
mailing list