Retrieve class name of caller

Koen Van Herck koen_van_herck at yahoo.com
Fri Sep 19 04:35:52 EDT 2003


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.




More information about the Python-list mailing list