Skip to content Skip to sidebar Skip to footer

Two Classes Differing Only In Base Class

I have come across a problem where I need two classes that will have identical implementation and the only difference between them will be different name and base class. What is a

Solution 1:

You can use multiple inheritance to implement interfaces and common functionality in a single mixin class. Given the clear desire to frobnicate in many classes, just implement a frobnicator. Python builds the class from right to left so mixins are left-most.

classFrobnicator(object):
    deffrobnicate(self):
        print("frob")

classFooA(Frobnicator, BaseA):
    passclassFooB(Frobnicator, BaseB):
    pass

Note that mixins usually do not implement their own __init__ - that's the job of the base class.

Post a Comment for "Two Classes Differing Only In Base Class"