Retrieve class name of caller

Bertrand Geston bergeston at yahoo.fr
Fri Sep 19 09:02:49 EDT 2003


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).





More information about the Python-list mailing list