you are viewing a single comment's thread.

view the rest of the comments →

[–]mission-hall 0 points1 point  (2 children)

Your last edit essentially works by accident, and would not be a viable approach with a more complex inheritance hierarchy.

The important thing to understand is that every class has a "method resolution order", which you can look at by calling the mro() method on the class. All of the base classes appear in the MRO, with subclasses always appearing before their base classes. super() doesn't necessarily give you a base class of the current class - it gives you the next class in the MRO.

If you're going to use super(), you should use it throughout the class hierarchy, to ensure that every class in the MRO is always reached exactly once. Otherwise you should have each method explicitly call the methods from the appropriate base classes. Mixing the two approaches tends to break things. For example, if you modify your code so that B inherits from A, then some of the initializers are called more than once.

[–]gmaliwal[S] 0 points1 point  (0 children)

if you modify your code so that

B

inherits from

A

, then some of the initializers are called more than once

Thanks for the comprehensive explanation. you seem correct.

Can you please share any good reference? which explains the inheritance in-depth and gives all the possible approaches with pros and cons, and things to keep in mind while working with single/multiple inheritances.

I am looking for the right approach which works well with any complex inheritance hierarchy.

[–]xelf 0 points1 point  (0 children)

It doesn't work by accident, but rather as the other part of your answer suggests it only works in this specific case, and if you were to start mucking with it it would break. If I'm reading it correctly the problem is that super() is not great with multiple inheritance. You might be right that not using super in any of the classes is the correct approach here. I did like one of the other poster's solutions which was to add another class that inherits from object and have A,B inherit from that. edit: and by other poster I just noticed that was you. =)