2
votes

Say I have a base class (MyBase) which has an __init__ method

I also have 2 mixins. Each of these mixins has a single attribute, and a couple of methods, but none of the methods or attributes are common in the two mixins.

MyBaseClass, mixin1, and mixin2 all inherit from object

I want to derive a class (MyRealClass) from the mixins (mixin1 and mixin2) and from MyBase.

From what I saw on MRO, the correct definition of MyRealClass would be

class MyRealClass(mixin1, mixin2, MyBase):

MyRealClass has a do_init method (I want to control when I initialize certain parts of the class, but MyBase has an __init__ method.

My question is, in the mixins, should I have an __init__ method in them?

Can you explain why they should? I don't need to do any work in the init for either mixin for my class.

Thanks

2

2 Answers

2
votes

Well, __init__ will be automatically invoked on your MyRealClass objects when they are created. Then, the function call is treated according to MRO: first, if MyRealClass defines __init__, it will be called, otherwise the interpreter will search for __init__ in the parent classes: Mixin1, Mixin2, and, finally, in MyBase. This means that MyRealClass does not need any __init__ method.

If you choose to implemet MyRealClass.__init__ at some point, make sure to call super(MyRealClass, self).__init__() or you will shadow the MyBase constructor.

0
votes

No. There's no point overriding a method if you're not doing anything in it. Just let it automatically inherit the superclass method.