Name of current method

Erik Max Francis max at alcyone.com
Thu Nov 28 14:48:44 EST 2002


Thomas Guettler wrote:

> How can I get the name of the method which is
> executed?
> 
> Example:
> 
> def mymethod(self):
>         print self.__magic__
> 
> --> "mymethod"

Function (and thus method) objects have a __name__ attribute (at least
in Python 2.2.2) which can be used.  But instead you seem to want to be
inside a method inside some class and decide where you are.  I'm not
aware of any builtin facility for this, but obviously you could do it
manually:

	class SomeClass:

	    ...

	    def myMethod(self, ...):
	        self.where = 'myMethod'
	        ...

	    def myOtherMethod(self, ...):
	        self.where = 'myOtherMethod'

	    ...

But this approach obviously isn't very elegant.  It sounds like if you
think you need this facility, there's probably a better way to
accomplish whatever it is you're setting out to do.  What exactly do you
think you need this for?

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ Some mistakes we must carry with us.
\__/ Speaker-to-Animals
    Blackgirl International / http://www.blackgirl.org/
 The Internet resource for black women.



More information about the Python-list mailing list