NeedB, that implement each others' abstract methods:
class NeedA(metaclass=abc.ABCMeta): @abc.abstractmethod def need_A(self): pass def need_B(self): pass class NeedB(metaclass=abc.ABCMeta): @abc.abstractmethod def need_B(self): pass def need_A(self): passNow, how do you create a subclass, through multiple inheritance, that inherits both ABCs such that their abstract method requirements are met by each other? You can't do the naive solution of inheriting both because whomever comes first in the inheritance chain will trigger a
class SubClass(NeedA, NeedB): pass SubClass() # Raises TypeError for NeedA.need_A not being implemented.
TypeErroris triggered because
NeedB.need_A(), preventing the abstract method from being implemented.
Luckily it is easy to get around this:
class SubClass(NeedA, NeedB): need_A = NeedB.need_A
By explicitly pulling up into the subclass the shadowed method needed to meet the abstract method requirement you fix the error. This trick works because
classstatements essentially execute the code contained within them, making any created variables class variables; this obviously includes assignment.
I came across this problem myself and it took my a second to figure out how best to solve it w/o trying some crazy metaclass that searched both up and down the inheritance tree looking for methods that implement an abstract method.