Saturday 29 September 2007 — This is more than 17 years old. Be careful.
I have long been confused by the super() function in Python. It’s used to call a method on the base class of an object. Why not simply invoke the base class directly, like we used to?
class Derived(Base):
def __init__(self):
super(Derived, self).__init__() # Right
# Base.__init__(self) # WRONG!
And shouldn’t the argument to super be Base, not Derived? It doesn’t seem to make any sense. I finally was truly confused when deriving a class from two base classes. If I only have one call to super, how do I make sure that both base class methods get called? Poking around for an answer, I finally found a good explanation of what super() is for. Ironically, it is a polemic about why super() is no good: Python’s Super Considered Harmful. Skip the part about Dylan, and read about the Python, and finally understand...
It turns out multiple inheritance is where everything is cleared up, and where the problems are that super() is actually solving. In a strictly single-inheritance world, the old style of calling the base class methods works fine. In multiple inheritance, you can’t actually predict what class you’ll call.
Comments
At least, that's what I tell people :)
>>> class A:
... def hello(self):
... print("Hi")
...
>>> class B(A):
... def hello(self):
... super().hello()
... print("There")
...
>>> b = B()
>>> b.hello()
Hi
There
>
Add a comment: