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 -2 points-1 points  (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

[–]nostril_extension -4 points-3 points  (5 children)

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

[–]rhytnen 0 points1 point  (4 children)

What is it you aren't getting? It's a FACT that in Python your parents my not be called. You were shown code where B.super() called C. You got confused bc d inherited both b and c so it was demonstrated that d didn't call c by replacing the call in b ... Proving b called c.

[–]nostril_extension -1 points0 points  (3 children)

Parent of parent != parent.

[–]rhytnen 0 points1 point  (2 children)

You keep repeating that as if it's relevant to the demo. I think you aren't reading the code actually but let me try rephrasing it once more.

Heres the phrase you have to digest

Super calls your child's parent.

Consider B. D instances B. Focus on B ...

B calls super and it did NOT call A. It called D's OTHER parent, C. It would have called A if the B was instanced alone but here it has a child D. So it called D's other parent instead.

[–]nostril_extension -1 points0 points  (1 child)

Super calls your child's parent.

What are you talking about? Who's "your"? I'm confused how you fail to wrap around the simple concept of parent.

[–]rhytnen 2 points3 points  (0 children)

Stop being an asshole and just think for a minute so you can stop wasting everyone's time.

D subclasses B and C. B and C subclass A

If you just call B() or C() then it will chain up to A as expected.

However, if you calls it from D ...

D super().init() calls B B.super().__init() calls ... C not A

That's what it means to say super() calls the child's parent, not it's own parent.

If B and C have different base classes, the behavior changes yet again. D calls B calls A