Retrieve class name of caller
Koen Van Herck
koen_van_herck at yahoo.com
Mon Sep 22 04:54:18 EDT 2003
Bertrand Geston <bergeston at yahoo.fr> wrote in message news:<bkeuos$18vsv$1 at sinclair.be.wanadoo.com>...
> Koen Van Herck wrote:
> > For debugging/logging purposes, I have a function
> >
> > def Log(msg):
> > print '%s.%s: %s' % (cls, method, msg)
> >
> > I call this function from a class method, and I would like to retrieve
> > the name of the caller method and its class. I already came up with
> >
> > method = sys._getframe(1).f_code.co_name
> > cls = sys._getframe(1).f_locals['self'].__class__
> >
> > I wonder if there are better ways to retrieve this info. In
> > particular, my implementation assumes that the instance parameter of
> > the method is called 'self', which it usually is, of course.
> >
> > Regards,
> > Koen.
>
> Take look to the inspect module. Could be what you need (and even more).
OK. But if I have the frame or code object, how can I get the method
or function object? For example, for a method m I have code object c:
c = m.im_func.func_code
How can I find m from c ?
More information about the Python-list
mailing list