Getting method name from within the class method

Larry Bates larry.bates at websafe.com
Wed Oct 18 17:53:38 EDT 2006


Mitko Haralanov wrote:
> I need to be able to get the name of the currently executed method
> within that method. I know that the method object does have the
> __name__ attribute but I know know how to access it from withing the
> method.
> 
> Something like this:
> 
> class A:
> ....
>     def a_method (self, this, that):
>          print <__name__>
> 
> a = A ()
> a.a_method()
> 'a_method'
> 
> 
> Thanx for your help!

Since you KNOW you are in the method, you can hardcode it:

    def a_method(self, this, than):
        print "a_method"

There's nothing to be gained from accessing a variable unless
you are going to be calling some other method to do the printing.

-Larry



More information about the Python-list mailing list