find class where method was defined ?

Simon Burton simon at arrowtheory.com
Tue Sep 19 19:43:17 EDT 2006


def methclass(meth):
    cls = meth.im_class
    name = meth.im_func.__name__
    meth = getattr(cls,name,None)
    for cls in cls.mro():
        _meth = getattr(cls,name,None)
        if _meth is not None and _meth==meth:
            result = cls
    return result


>>> methclass(b.foo)
<class '__main__.A'>


(Turns out equality bypasses the methodwraper trickery.)

Simon.




More information about the Python-list mailing list