Python super() arguments: why not super(obj)?
Solution 1
The two-argument form is only needed in Python 2. The reason is that self.__class__
always refers to the "leaf" class in the inheritance tree -- that is, the most specific class of the object -- but when you call super
you need to tell it which implementation is currently being invoked, so it can invoke the next one in the inheritance tree.
Suppose you have:
class A(object):
def foo(self):
pass
class B(A):
def foo(self):
super(self.__class__, self).foo()
class C(B):
def foo(self):
super(self.__class__, self).foo()
c = C()
Note that c.__class__
is C
, always. Now think about what happens if you call c.foo()
.
When you call super(self.__class__, self)
in a method of C, it will be like calling super(C, self)
, which means "call the version of this method inherited by C". That will call B.foo
, which is fine. But when you call super(self.__class__, self)
from B, it's still like calling super(C, self)
, because it's the same self
, so self.__class__
is still C
. The result is that the call in B will again call B.foo
and an infinite recursion occurs.
Of course, what you really want is to be able to call super(classThatDefinedTheImplementationThatIsCurrentlyExecuting, self)
, and that is effectively what the Python 3 super()
does.
In Python 3, you can just do super().foo()
and it does the right thing. It's not clear to me what you mean about super(self)
being a shortcut. In Python 2, it doesn't work for the reason I described above. In Python 3, it would be a "longcut" because you can just use plain super()
instead.
The super(type)
and super(type1, type2)
uses might still be needed occasionally in Python 3, but those were always more esoteric usages for unusual situations.
Solution 2
Trying a short answer:
self.__class__
is always the actual ("sub-most") class of your object instance – not necessarily the wanted class, which implements the function!
Replace super(self.__class__, self)
with super(__class__, self)
and you are right within a method definition in Python 3, because Python 3 provides the magic cell variable __class__
for the implementing class.
And simply super()
with zero arguments is already the shortcut for super(__class__, self)
in Python 3. See PEP3135.
Python 2 neither knows __class__
nor the zero-argument shortcut super()
.
![Gabriel](https://i.stack.imgur.com/8Vbhv.jpg?s=256&g=1)
Gabriel
Updated on June 06, 2022Comments
-
Gabriel about 2 years
I am trying to understand when and how to use super() in Python correctly (either 2.7.x or 3.x)
on
>>> help(super)
the interpreter tells me how to call it:class super(object) | super(type) -> unbound super object | super(type, obj) -> bound super object; requires isinstance(obj, type) | super(type, type2) -> bound super object; requires issubclass(type2, type)
I understand that in Python3.x it's now possible to juse use super() within a class definition, but I don't understand why
super(obj)
is not possible. Orsuper(self)
within a class definition.I know there must be a reason for it, but I can't find it. To me those lines are equivalent to
super(obj.__class__, obj)
orsuper(self.__class__, self)
and those would work right?I would think that just typing
super(obj)
would be a nice shortcut even in Python 3.x. -
user4815162342 almost 11 yearsA coworker once refactored a piece of a method that called
super
into a separate method in another class, so the code ended up sayingsuper(SomeOtherClass, some_obj).method(...)
That worked perfectly, but it looked just like a bug to anyone who read the code. Although this could have been (and ended up being) expressed better, the two-argument form ofsuper
can still be useful in some edge cases. -
Gabriel almost 11 yearsThe first 4 lines of your explanation made me understand everything. Thank you for the example as well. That was the missing piece that I was trying to understand.
-
Chris Conlan almost 7 yearsWe keep referring to an 'esoteric' or 'edge' case. Is this simply when double inheritance requires we call two separate
__init__()
functions when overriding__init__()
in the leaf class? -
BrenBarn almost 7 years@ChrisConlan: The cases being referred to there are cases where you are calling super with two types rather than a type and an instance. This would be for instance, if you want to call a classmethod rather than instance method. See this question.
-
user48956 about 4 yearssuper() (no-args) is a little surprising. The PEP says its not a keyword, so its a little unclear exactly how it finds class and self.
-
pallgeuer about 4 years@user48956
super()
is equivalent tosuper(__class__, <firstarg>)
. Wherever the call tosuper()
is, look at the method (function defined directly within a class body) it is defined in, and substitute__class__
with that class, and<firstarg>
with the first argument of that method. -
pallgeuer about 4 yearsWhat tripped me up (making me Google this question) is that if you define a local function that calls
super()
inside a method of class A, but make that local function a method of another class B, thensuper()
will resolve__class__
to be A, not dynamically resolve it at execution time to be B.