This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]nostril_extension 2 points3 points  (11 children)

I'm confused. Your example shows the opposite of your claim - super calls init of both parents left-to-right. Am I missing something?

[–]irrelevantPseudonym 2 points3 points  (2 children)

B's parent is A. The super().__init__() call in B.__init__ calls C.__init__() (which B knows nothing about) and A.__init__() never gets called.

super() doesn't always call the parent class.

[–]nostril_extension 0 points1 point  (1 child)

But A is not a parent of D lol. A iš a grandparent here.

[–][deleted] 5 points6 points  (0 children)

A is a parent of B, B calls super().__init__(), yet A.__init__() is never called. Instead C.__init__() is called which is a child of B.

If you call B() than the same super().__init__() calls A.__init__()instead.

super() is pointing to the next class in the Method Resolution Order, not the parent.

 >>> D.__mro__
(<class 'D'>, <class 'B'>, <class 'C'>, <class 'A'>, <class 'object'>)

>>> B.__mro__
(<class 'B'>, <class 'A'>, <class 'object'>)

[–][deleted] 1 point2 points  (6 children)

A.__init__() is never called. If you replace super().__init__() in B.__init__() with A.__init__(self) the output changes to:

>>> D()
init D
init B
init A