How to get the method/function which called a function?

Chris Liechti cliechti at gmx.net
Wed Jul 24 12:54:23 EDT 2002


holger krekel <pyth at devel.trillke.net> wrote in
news:mailman.1027527338.25170.python-list at python.org: 

> Alessandro Iob wrote:
>> def log(msg):
>>      caller = GET_CALLER()
>>      print repr(caller), msg
>> 
>> class A:
>>      def t(self):
>>           log('a message')
>> 
>> a = A()
>> a.t() # prints "<bound method A.t of <__main__.A instance at
>> 0x805bb64>> a message"
>> 
>> How can I implement the "GET_CALLER()" function in "log()" ?
> 
> You can't.

sure you can get the name of the calling function. its printed when a 
exception is thrown, isn't it? ;-)

so one way is so a "try: raise except: ..." and then inspect the traceback 
using the module of the same name.
however i think i saw more elegant soltions a while ago in a thread. maybe 
a search on groups.google.com brings them up.

chris
-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list