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.

class Frobnicator(object):
    def frobnicate(self):
        print("frob")

class FooA(Frobnicator, BaseA):
    pass

class FooB(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"