· 

Python: super()

1. super()

Suppose class A inherits from class B, B inherits from class C, C inherits from class D, and D inherits from class E.  E has a form_valid(). 

 

C has another form_valid(), and A has another form_valid().  This means C's form_valid() overrides E's, and A's form_valid() overrides C's. 

 

In this situation, A has a line of "return super().form_valid()".  Which does super().form_valid() mean, C's form_valid() or E's form_valid()?  What does super() exactly mean?  The closest parent, or the farthest parent? 

super().form_valid() means the highest parent's form_valid, right?

 

2. super() is the closest parent

Great question! Let's clarify this thoroughly.


❓ Your question:

If we have this class inheritance:

kotlin
class A(B) class B(C) class C(D) class D(E)

And:

  • E has form_valid()

  • C overrides form_valid()

  • A also overrides form_valid()

Then in A, you call:

python
return super().form_valid(form)

✅ What does super() mean?

super() does not mean "highest parent".
It means: "the next class in the Method Resolution Order (MRO) after the current class."

So in your case:

python
class A(B):
def form_valid(self, form):
# some logic
return super().form_valid(form) # 👈 This calls B’s form_valid() if it exists
  • If B doesn’t define form_valid(), Python moves to C.

  • If C defines form_valid(), it is C.form_valid() that gets called.

  • It never jumps directly to E, skipping over C and D.


🔁 How Python decides this:

Python uses something called the Method Resolution Order (MRO).
You can check it like this:

python
print(A.__mro__)

This shows the order Python searches for methods.


✅ In short:

  • super() always means: "next class in the MRO."

  • super().form_valid() in A calls C.form_valid(), not E.form_valid(), assuming B has no such method.

  • So: It is the closest next method, not the farthest one.

 

Let me know if you'd like a code example to test this live!