The super() function is how a class reaches the behaviour that it inherits, and it is the tool that makes overriding safe. Used properly, it lets a subclass extend a parent's method instead of replacing it, and it keeps working even when there is more than one parent in the chain.
The previous lesson in this module covered "Inheritance: Extending Without Copy-Pasting", and the next lesson in this module is about "Multiple Inheritance and the MRO".
Two ideas sit underneath this page. The first one is what super() actually refers to, and the second one is what happens when a chain of classes each call it. Both matter more than the syntax, because the syntax is easy and the chain is where the bugs live.
The word "super" is misleading, because super() does not mean "my parent class". It means "the next class in the method resolution order after this one", which is the class that Python would look in next when it resolves a name. For a class with a single parent, that happens to be the parent, which is why the simpler mental model usually works right up until you write multiple inheritance.
What does super() actually give you?
super() returns a proxy object that stands in for whichever class sits next in the MRO. Calling super().method(args) invokes that class's method, with self still bound to your current object. You never pass self yourself.
class Logger:
def __init__(self, name):
self.name = name
def log(self, message):
return '[' + self.name + '] ' + message
class Service(Logger):
def __init__(self, name, url):
super().__init__(name)
self.url = url
def log(self, message):
return super().log(self.url + ' -> ' + message)
svc = Service('api', 'https://example.com')
print(svc.name)
print(svc.url)
print(svc.log('request sent'))
Notice the shape of the call. It is super().__init__(name) and not super().__init__(self, name). The self is supplied for you, and passing it a second time is an error that shows up as a confusing message about the number of positional arguments.
There is a small practical benefit to using super() even when you have exactly one parent. It keeps the code correct if you later add a second parent. Writing Parent.method(self) hard-codes the parent class into the body of the child, and that line becomes wrong the moment the shape of the hierarchy changes.
How do you extend a method instead of replacing it?
Call the version that super() gives you first, and then add your own behaviour to the result. That is the pattern behind every "decorate and delegate" method that you will write. You compute the base value, you transform it, and you return it.
class Invoice:
def __init__(self, subtotal):
self.subtotal = subtotal
def total(self):
return self.subtotal
class DiscountedInvoice(Invoice):
def __init__(self, subtotal, percent):
super().__init__(subtotal)
self.percent = percent
def total(self):
return super().total() * (1 - self.percent / 100)
print('standard:', Invoice(200).total())
print('discounted:', DiscountedInvoice(200, 25).total())
Compare the two classes. Invoice inherits total() unchanged. DiscountedInvoice overrides it, calls super().total() to get the amount before the discount, and applies its own reduction. If the parent's calculation ever changes, the subclass follows it automatically.
The alternative, which is writing Parent.total(self) explicitly, also works. It hard-codes a parent though, and it breaks the moment a second parent appears in the chain.
How does super() behave in an __init__ chain?
Each class in the chain calls super().__init__() and passes along whatever it received, together with its own extra arguments. The chain stops when a class's __init__ does not call super() at all, or when it reaches object, whose __init__ takes no arguments.
class User:
def __init__(self, name):
self.name = name
class Customer(User):
def __init__(self, name, email):
super().__init__(name)
self.email = email
class PremiumCustomer(Customer):
def __init__(self, name, email, tier):
super().__init__(name, email)
self.tier = tier
p = PremiumCustomer('Asha', '[email protected]', 'gold')
print(p.name, p.email, p.tier)
print('all three attributes are set:', vars(p))
There are two things to notice here. The arguments are forwarded as you go up the chain, so PremiumCustomer takes a tier and passes a name and an email to Customer.__init__, which takes a name and passes it on. And the order of execution runs from the bottom up. The body of the most derived class runs first, then the parent's body, and then the grandparent's body.
That ordering matters when the child does work in __init__ that depends on the parent's attributes. The parent's __init__ has not run yet at the start of the child's body, so anything that the parent sets does not exist yet. Set your own attributes after the super() call, or validate your input before it.
What happens if a class forgets to call super()?
The chain stops there. Any parent below that point never gets initialised, so its attributes are missing, and the failure appears later and somewhere else as an AttributeError that has nothing to do with the real mistake.
class User:
def __init__(self, name):
self.name = name
self.level = 'standard'
def greet(self):
return 'hello ' + self.name + ' (' + self.level + ')'
class Sloppy(User):
def __init__(self, name):
self.name = name
try:
print(Sloppy('Ben').greet())
except AttributeError as error:
print('AttributeError:', error)
The error message in that traceback points at the line that reads the missing attribute, and not at the class that forgot to call super(). That is why "always call super().__init__()" is good advice even when you think you do not need the parent's setup. The parent might set up something that you have not noticed yet.
There is a second failure mode worth knowing about, which is a subclass that calls super().__init__() with the wrong arguments. The parent's signature is the contract, and when a grandchild later appears in the chain, the parent's signature is the one that everything has to keep matching. If you need a different setup, give the parent a keyword argument with a default value rather than changing the order of the positional ones.
Is super() only for __init__?
No. It works in any method, and it is how a subclass shares behaviour across siblings that inherit from the same parent. People sometimes call this the mixin pattern, where several small classes each contribute a piece of behaviour and the concrete class at the end combines them.
class BaseProcessor:
def process(self, item):
return 'processed ' + item
class LoggingMixin:
def process(self, item):
return 'logged(' + super().process(item) + ')'
class TimingMixin:
def process(self, item):
return 'timed(' + super().process(item) + ')'
class Worker(LoggingMixin, TimingMixin, BaseProcessor):
def process(self, item):
return super().process(item)
print(Worker().process('job-7'))
Every one of those methods calls super() and hands its value onward, so the chain composes cleanly. If one of them forgot to call super(), then the methods below it in the chain would never run, and the combined behaviour would be quietly incomplete.
What to carry forward
super()means "next in the MRO" and not "my parent". The two coincide in simple cases.- Never pass
selftosuper(), because it is supplied for you. - Call
super()inside__init__and forward your extra arguments up the chain. - To extend rather than replace, compute the base result and then transform it.
- A missing
super()call breaks the chain quietly, and the error surfaces far away from the real mistake.
Try it yourself: build a cooperative chain
Write three classes. The first is Notifier, which has an __init__ that takes a channel and a send(message) method that returns the message wrapped in the channel name. The second is LoggingNotifier and the third is RetryingNotifier, and both of them inherit from Notifier and override send() so that they call super().send() first.
Now write a fourth class that inherits from LoggingNotifier and calls super().send() again, so that the three behaviours stack. Check that the final output contains all three contributions. Then temporarily delete the super() call from the middle class, run it again, and note which part of the behaviour disappears. That missing piece is exactly what a broken chain costs you.