Python isn’t purely an object-oriented OOP language. One OOP concept that gives python more power is Inheritance
In this tutorial, you’ll learn about the following:
- The concept of inheritance in Python
- How the
- How the
super()function in single inheritance works
- How the
super()function in multiple
If you have experience with object-oriented languages, you may already be familiar with the functionality
If not, don’t fear! While the official documentation is fairly technical, at a high level
super()gives you access to methods in a superclass from the subclass that inherits from it.
super() alone returns a temporary object of the superclass that then allows you to call that superclass’s methods.
Why would you want to do any of this? While the possibilities are limited by your imagination, a common use case is building classes that extend the functionality of previously built classes.
Calling the previously built methods
super() in Single Inheritance
Inheritance is a concept in object-oriented programming in which a class derives (or inherits) attributes and behaviors from another class without needing to implement them again.
Class Rectangle: def __init__(self, length, width): self.length = length self.width = width def area(self): return self.length * self.width def perimeter(self): return 2 * self.length + 2 * self.width Class Square: def __init__(self, length): self.length = length def area(self): return self.length * self.length def perimeter(self): return 4 * self.length
Here, there are two similar classes:
You can use them as below:
>>> square = Square(4) >>> square.area() 16 >>> rectangle = Rectangle(2,4) >>> rectangle.area() 8
In this example, you have two shapes that are related to each other: a square is a special kind of rectangle. The code, however, doesn’t reflect that relationship and thus has code that is essentially repeated.
By using inheritance, you can reduce the amount of code you write while simultaneously reflecting the real-world relationship between rectangles and squares:
Class Rectangle: def __init__(self, length, width): self.length = length self.width = width def area(self): return self.length * self.width def perimeter(self): return 2 * self.length + 2 * self.width # Here we declare that the Square class inherits from the Rectangle class Class Square(Rectangle): def __init__(self, length): super().__init__(length, length)
>>> square = Square(4) >>> square.area() 16
In this example,
Rectangle is the superclass, and
Square is the subclass.
.__init__() methods are so similar, you can simply call the superclass’s
.__init__() method (
Rectangle.__init__()) from that of
Square by using
super(). This sets the
.width attributes even though you just had to supply a single
length parameter to the
When you run this, even though
Some more Info
While the examples above (and below)
super() can also take two parameters: the first is the subclass, and
First, let’s see two examples showing what manipulating the first variable can do, using the classes already shown:
class Rectangle: def __init__(self, length, width): self.length = length self.width = width def area(self): return self.length * self.width def perimeter(self): return 2 * self.length + 2 * self.width class Square(Rectangle): def __init__(self, length): super(Square, self).__init__(length, length)
In Python 3,
super() with other classes as well:
class Cube(Square): def surface_area(self): face_area = super(Square, self).area() return face_area * 6 def volume(self): face_area = super(Square, self).area() return face_area * self.length
In this example, you are setting
Square as the subclass argument to
super(), instead of
Cube. This causes
super() to start searching for a matching method (in this case,
.area()) at one level above
Square in the instance hierarchy, in this case
In this specific example, the behavior doesn’t change. But imagine that
Square also implemented an
.area() function that you wanted to make sure
Cube did not use. Calling
super() in this way allows you to do that.
Caution: While we are doing a lot of fiddling with the parameters to
super() in order to explore how it works under the hood, I’d caution against doing this regularly.
The parameterless call to
super() is recommended and sufficient for most use cases, and needing to change the search hierarchy regularly could be indicative of a larger design issue.
What about the second parameter? Remember, this is an object that is an instance of the class used as the first parameter. For an example,
isinstance(Cube, Square) must return
By including an instantiated object,
super() returns a bound method: a method that is bound to the object, which gives the method the object’s context such as any instance attributes. If this parameter is not included, the method returned is just a function, unassociated with an object’s context.
For more information about bound methods, unbound methods, and functions, read the Python documentation on its descriptor system.
super() doesn’t return a method. It returns a proxy object. This is an object that delegates
Thats it in this Tutorial
Thank you for reading. let me know on the comment section how helpful it was.
If you like the Article you can give it a clap 👏.
If you feel like you owe me