Thanks to Sr2222 for not noticing the point ...
Here's an adjusted approach that is similar to Alex but does not require importing anything. I donβt think this is an improvement if there is no huge hierarchy of inherited classes, since this approach stops as soon as the defining class is defined, instead of returning the whole inheritance as getmro . As said, this is an unlikely scenario.
def get_class_that_defined_method(method): method_name = method.__name__ if method.__self__: classes = [method.__self__.__class__] else: #unbound method classes = [method.im_class] while classes: c = classes.pop() if method_name in c.__dict__: return c else: classes = list(c.__bases__) + classes return None
And an example:
>>> class A(object): ... def test(self): pass >>> class B(A): pass >>> class C(B): pass >>> class D(A): ... def test(self): print 1 >>> class E(D,C): pass >>> get_class_that_defined_method(A().test) <class '__main__.A'> >>> get_class_that_defined_method(A.test) <class '__main__.A'> >>> get_class_that_defined_method(B.test) <class '__main__.A'> >>> get_class_that_defined_method(C.test) <class '__main__.A'> >>> get_class_that_defined_method(D.test) <class '__main__.D'> >>> get_class_that_defined_method(E().test) <class '__main__.D'> >>> get_class_that_defined_method(E.test) <class '__main__.D'> >>> E().test() 1
Alex's solution returns the same results. While the Alex approach is used, I would use it instead.
estani Nov 29 2018-12-12T00: 00Z
source share