Is there a way to find the name of the method currently being executed?

Mike C. Fletcher mcfletch at rogers.com
Wed Sep 25 23:18:12 EDT 2002


This is a massive hack, and I'm not sure if it covers the corner cases, 
but oh well :),

PythonWin 2.2.1 (#34, Apr  9 2002, 19:34:33) [MSC 32 bit (Intel)] on win32.
Portions Copyright 1994-2001 Mark Hammond (mhammond at skippinet.com.au) - 
see 'Help/About PythonWin' for further copyright information.
 >>> import traceback
 >>> class a:
... 	def func1( self ):
... 		return traceback.extract_stack(limit=1)[0][2]
... 	def func2( self ):
... 		return traceback.extract_stack(limit=1)[0][2]
... 	
 >>> a().func1()
'func1'
 >>> a().func2()
'func2'
 >>>

HTH,
Mike

Craeg K Strong wrote:
> Hello:
> 
> class a:
> 
>    def func1(self):
>        print "func1 being called"
> 
>    def func2(self):
>        print "func2 being called"
> 
> In the code sample above, I would like to replace the "func1"
> and "func2" strings with constants so the same code could
> be copied verbatim into multiple functions and do the right
> thing.
> 
> Is there a way to do this in Python?  For example, maybe you could
> get your hands on the call stack and print out stack[0].__name__
> or something....
> 
> Thanks in advance,
> 
> --Craeg
> 
> BTW, I could not find this in the python.org FAQ....
> 
> 
> 
_______________________________________
   Mike C. Fletcher
   Designer, VR Plumber, Coder
   http://members.rogers.com/mcfletch/






More information about the Python-list mailing list