Accessing methods from superclasses
super
is a built-in class that can be used to access an attribute belonging to an object's superclass.
Note
The Python official documentation lists super
as a built-in function. But it's a built-in class, even if it is used like a function:
>>> super <class 'super'>
Its usage is a bit confusing when you are used to accessing a class attribute or method by calling the parent class directly and passing self
as the first argument. This is really old pattern but still can be found in some codebases (especially in legacy projects). See the following code:
class Mama: # this is the old way def says(self): print('do your homework') class Sister(Mama): def says(self): Mama.says(self) print('and clean your bedroom')
When run in an interpreter session it gives following result:
>>> Sister().says() do your homework and clean your bedroom
Look particularly at the...