I came across super(C, self) and associated it with Python 2. Is that syntax still useful in Python 3, and are Python 2 codebases still around?
Does seeing the old super() syntax mean a codebase is still on Python 2?
Answers
It doesn't tell you which Python version the code runs on. In an ordinary instance method on class C, super(C, self).method() and super().method() normally do the same thing. The explicit form still works in Python 3 and isn't deprecated.
Zero-argument super() is usually more convenient, but older style can stick around because of existing conventions or compatibility requirements.
Reply to this answer
Yes, Python 2 codebases still exist, though the syntax alone won't prove you've found one. Check the project's declared Python version and dependencies rather than guessing from a super() call.
Python 2 is past its official support lifetime, so a project that still needs it also needs a plan for the security and maintenance risks.
Reply to this answer
The explicit form can still be handy when zero-argument super() doesn't have the class and instance context it needs. For example, a function defined outside the class and later attached as a method may need super(C, self).
You can also deliberately choose a different starting point in the method resolution order, though that's a more specialized use and can make the code harder to follow.
Reply to this answer
One detail that trips people up is that super() doesn't simply mean โcall my parent.โ It looks for the next implementation in the method resolution order. With multiple inheritance, that can be a sibling class rather than the direct parent you'd expect.
Calling Base.__init__(self) directly can work for simple inheritance, but it doesn't participate in cooperative dispatch in the same way. That's where consistent super() calls across the classes matter.
folded.napkin+:
Looking at the class's __mro__ can help when the next method isn't obvious.
Reply to this answer
Add your answer
Answers are reviewed before publication.
BUTTON.sideways:
Good to know. I was treating the explicit arguments as a Python 2-only feature.