python: how to get the class of a calling method through inspection?
For my exception class I'd like to find out whether the function which instantiated the exception object is a method and if so, show the class name.
So in the init method of my exception class I get the name of the calling function:
frame, module, line, function, context, index = inspect.stack()[1]
But is there a开发者_运维问答ny way the get the class name (if any) of the calling function?
Assuming the frame is for an instance method:
self_argument = frame.f_code.co_varnames[0] # This *should* be 'self'.
instance = frame.f_locals[self_argument]
class_name = instance.__class__.__name__
精彩评论