Accessing method object from within a method

Peter Hansen peter at engcorp.com
Wed Feb 18 22:23:57 CET 2004


Vivek Sawant wrote:
> 
> Is there a way to obtain the method/function object from within the
> method like 'self' refers to the object instance. I would like to avoid
> looking method name (string) in class/object attributes. My goal is to
> write code that need not be changed when the method name is changed.
> 
> def method (self, ...)
>    # somehow obtain 'methodobj' for this 'method'
>    mname = methodobj.__name__;
>    # do something with mname

The usual use for this is for debugging purposes, when somebody wants
to print the name of the current method.  If your needs are not much
different than that, look into sys._getframe() and frame objects.

-Peter



More information about the Python-list mailing list