Skip to content Skip to sidebar Skip to footer

How To Check In Python From Which Class Methods Is Derived?

I have two classes: class A(object): def a(self): pass class B(A): def b(self): pass print dir(A) print dir(B) How can I check from which class methods is derived in

Solution 1:

Interesting question. Here is how I'd go about it.

(This works in python2. I haven't tested it in python3, but I won't be surprised if it does not work...)

You can iterate over all the "nominees" using reversed(inspect.getmro(cls)), and returning the first (by fetching the next value of the iterator) which satisfy the condition that it has the relevant attr, and that attr is the same as the method of the relevant cls.

Method identity-comparison is done by comparing the im_func attribute of the unbound method.

import inspect

defgetMethodClass(cls, attr):
   returnnext(
      basecls for basecls in reversed(inspect.getmro(cls))
      if hasattr(basecls, attr)
      and getattr(basecls, attr).im_func is getattr(cls, attr).im_func
   )

getMethodClass(A, 'a')
=> __main__.A
getMethodClass(B, 'a')
=> __main__.A
getMethodClass(B, 'b')
=> __main__.B

# an alternative implementation, suggested by @chameleondefgetAttributeClass(cls, attrName):
  # check first if has attribute
  attr = getattr(cls, attrName)

  mro = inspect.getmro(cls)
  # only one class on listif len(mro) == 1:
    return cls

  # many class on listfor base in reversed(mro[1:]):
    # check if defined in this basetry:
      baseAttr = getattr(base, attrName)
    except AttributeError:
      continue
    else:if baseAttr.im_func is attr.im_func:return base
  # define in top classreturn cls

The function can also have the signature you suggest:

defgetMethodClass(unbound_method):
    cls = unbound_method.im_class
    attr = unbound_method.__name__
    # rest of implementation is the same as before...

getMethodClass(B.a)
=> __main__.A

Post a Comment for "How To Check In Python From Which Class Methods Is Derived?"