def get_class_that_defined_method(meth):
"""Determines the class owning the given method.
"""
if is_classmethod(meth):
return meth.__self__
if hasattr(meth, 'im_class'):
return meth.im_class
elif hasattr(meth, '__qualname__'):
# Python 3
try:
cls_names = meth.__qualname__.split('.<locals>', 1)[0].rsplit('.', 1)[0].split('.')
cls = inspect.getmodule(meth)
for cls_name in cls_names:
cls = getattr(cls, cls_name)
if isinstance(cls, type):
return cls
except AttributeError:
# If this was called from a decorator and meth is not a method, this
# can result in AttributeError, because at decorator-time meth has not
# yet been added to module. If it's really a method, its class would be
# already in, so no problem in that case.
pass
raise ValueError(str(meth)+' is not a method.')
评论列表
文章目录