Search results
The design of multiple inheritance is really really bad in python. The base classes almost need to know who is going to derive it, and how many other base classes the derived will derive, and in what order... otherwise super will either fail to run (because of parameter mismatch), or it will not call few of the bases (because you didn't write super in one of the base which breaks the link)!
This is why your code didn't work correctly. Because of the way diamond inheritance works in python, classes whose base class is object should not call super().__init__(). As you've noticed, doing so would break multiple inheritance because you end up calling another class's __init__ rather than object.__init__().
Jan 8, 2013 · Multiple inheritance super() initialization with different signatures -1 I have a multiple classes in python i.e class a, class b, class c, class d then how can i accassed the specific class method
Oct 5, 2011 · Python multiple inheritance is like a chain, in Child class mro, the super class of ParentA is ParentB, so you need call super().__init__() in ParentA to init ParentB. If you change super().__init__('ParentA') to Base.__init__(self, 'ParentA'), this will break the inheritance chain, output: Child called by Construct.
One of the common ways of designing your base classes for multiple inheritance, is for the middle-level base classes to accept extra args in their __init__ method, which they are not intending to use, and pass them along to their super call. Here's one way to do it in python: def __init__(self,a): self.a=a.
Jan 30, 2020 · The solution is simple: just add a base class that defines this method as a noop, and make all your composable dataclasses inherit from it: class Base(object): def __post_init__(self): # just intercept the __post_init__ calls so they. # aren't relayed to `object`. pass. @dataclass.
This is known as "diamond inheritance" and it is a big problem for many multiple inheritance systems (like in C++). Python's collaborative multiple inheritance (with super() ) lets you solve easily it in many cases (though that's not to say a cooperative multiple inheritance hierarchy is easy to design or always a good idea).
Aug 7, 2020 · This is the major pitfall IMHO with multiple inheritance in Python, and I generally advise against it unless you have zero-argument initializers. If you try to explicitly call each base class initializer (e.g. Player.__init__), then you will end up with some of your initializers executing multiple times.
Mar 2, 2015 · You could also set the other class's metaclass to ABCMeta so that all the multiple base classes's metaclass are ABCMeta. I had a similar problem when using multiple inheritance with: a class having a custom metaclass; a class having ABCMeta as metaclass; what I did to solve this was make my custom metaclass derive from ABCMeta.
If you need to alter class inheritance based on a value in the constructor, create two separate classes, with different structures. Then provide a different callable as the API to create an instance: class CA(A): # just inherit __init__, no need to override. class CB(B):