Getting method name from within the class method

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu Oct 19 00:47:10 CEST 2006


In <mailman.722.1161208770.11739.python-list at python.org>, Mitko Haralanov
wrote:

> On 18 Oct 2006 14:38:12 -0700
> yellowalienbaby at gmail.com wrote:
> 
>> >>> class test(object):  
>> ...  def a_method(self,this,that):
>> ...   print self.a_method.__name__
> 
> Doing the above will obviously work!
> 
> However, I don't want to have to use the name of the function in the
> print statement (the ".a_method." part). Imagine having about 100 of
> the above print statements in the function and then you change the name
> of the function. I want all 100 of the print statements to work without
> having to change every one of them to reflect the new function name.

class test(object):
    def a_method(self, this, that):
        NAME = 'a_method'
        print NAME
        # ...
        print NAME
        # ...

And please don't come back and complain that you have to type and probably
change 'a_method' twice instead of once.  ;-)

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list