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:
And:
-
Ehasform_valid() -
Coverridesform_valid() -
Aalso overridesform_valid()
Then in A, you call:
✅ 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:
-
If
Bdoesn’t defineform_valid(), Python moves toC. -
If
Cdefinesform_valid(), it isC.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:
This shows the order Python searches for methods.
✅ In short:
-
super()always means: "next class in the MRO." -
super().form_valid()inAcallsC.form_valid(), notE.form_valid(), assumingBhas 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!
