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

holger krekel pyth at devel.trillke.net
Wed Jul 24 17:41:49 EDT 2002


Chris Liechti wrote:
> 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? ;-)

oh, yes, you are right.  I probably was shortcircuiting on some 
other (only seemingly related) introspection problem.  Which
i can't even rememeber.  Maybe i need better introspection myself.

    holger




More information about the Python-list mailing list