Although Python is not strictly an object-oriented programming language, it is strong and flexible enough to let you use the object-oriented model to construct your programs. Python accomplishes this in part by allowing inheritance, which it ensures through super(). The super() built-in produces a surrogate entity (a transient superclass object) which we may use to reach the base class functions. In addition, the super() method grants the right to use to a parental or sibling class functions and attributes. Dealing with Simultaneous Inheritance helps us to evade having to use the parent class name directly. In this tutorial, you will be guided about invading a superclass using a super() method.

Syntax:

super()

Installation of Python:

Before implementing Python examples, we must have the Python package installed on our Ubuntu 20.04 Linux system. So, log in from the Linux system and open the terminal shell via “Ctrl Alt T” quickly. But, first, we have to check which version of Python has already been installed on our system. And, for this purpose, try out the query below. You can see, it is now showing the latest version of Python.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/1-9.png" data-lazy- height="37" src="data:image/svg xml,” width=”470″>

After knowing this, we need to update our apt repository first. Hence, try out the below query to update the Linux system:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/2-9.png" data-lazy- height="54" src="data:image/svg xml,” width=”620″>

Through the PPA package, add some additional packages for Python:

$ sudo add-apt-repository ppa:deadsnakes/ppa

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/3-9.png" data-lazy- height="40" src="data:image/svg xml,” width=”702″>

Press the Enter key to proceed:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/4-9.png" data-lazy- height="75" src="data:image/svg xml,” width=”707″>

It’s time to install the latest version of Python in our Linux system. So, try the below instruction in the console shell and press the Enter key:

$ sudo apt install python3.9

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/5-9.png" data-lazy- height="71" src="data:image/svg xml,” width=”552″>

Press “Y” to continue the installation process, otherwise hit the key “n” to stop it.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/6-6.png" data-lazy- height="72" src="data:image/svg xml,” width=”717″>

After the installation of the updated version of Python, it’s time to install the pip repository to use Python in our system. First of all, we need to verify if it is already installed or not. For this, let’s check the version of the pip package via the instruction shown below. The output illustrated that we don’t have a pip installed in our system.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/7-6.png" data-lazy- height="109" src="data:image/svg xml,” width=”476″>

To install the latest version of pip, we need a curl package in our Linux system. So, install it via the below query:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/8-5.png" data-lazy- height="68" src="data:image/svg xml,” width=”506″>

Download the pip package using curl in the console as follows:

$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/9-4.png" data-lazy- height="92" src="data:image/svg xml,” width=”715″>

Check the list of all the packages starting from the “get” keyword.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/10-3.png" data-lazy- height="39" src="data:image/svg xml,” width=”387″>

Now, execute the “get-pip.py” package just downloaded in the system via sudo query. It may take a while to install.

$ sudo python3.9 get-pip.py

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/11-2.png" data-lazy- height="72" src="data:image/svg xml,” width=”609″>

The output below is showing that the pip’s newest version has been installed efficiently.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/12-2.png" data-lazy- height="36" src="data:image/svg xml,” width=”389″>

Now you can check the version of the pip repository through the instruction below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/13-2.png" data-lazy- height="34" src="data:image/svg xml,” width=”661″>

Super Example 01:

Lastly, we will have a look at a mini example of a super function in Python. So, first of all, we need to create a “py” extension file from the command-line shell. Therefore, we will use the following touch query in the shell along with the name of a file as “one.py”:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/14-2.png" data-lazy- height="19" src="data:image/svg xml,” width=”429″>

Now, open the home directory of your system from the File Explorer. You will find the “one.py” file there. Open this file and add the Python script to it, as shown below. Let’s familiarize ourselves with this code first. At the very start, we have created a parent class named “Animal” with a constructor in it. This constructor is printing the name of any Animal and a string text that will be passed to it in a parameter in the near future. After that, we have created two child classes, “Birds” and “Other”. Both the child classes have constructors in their body having print some statements. These constructors are calling the superclass instructor while passing it their child name as the parameter value. Due to this, the parent class constructor will run and print the statement in its body along with the name of a child class. Object b1 and c1 have been used to call the child classes, respectively.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/15-2.png" data-lazy- height="342" src="data:image/svg xml,” width=”576″>

Let’s execute the Python script file to see the output of a code. For execution, write the following instruction in the shell preceded by the keyword python3 along with the name of a file as “one.py”. The output below indicates the first line from a child class “Birds” and the second line from the superclass “Animal”. The third line indicates the statement from child class “Other”, and the fourth line is from superclass “Animal”. Both the second and fourth lines have the first word from the parameter values passed from the child classes.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/16-2.png" data-lazy- height="104" src="data:image/svg xml,” width=”458″>

Super Example 02:

Open the same file, “one.py”, and update the code with the following code in it. This code contains one parent class as “Human” and one child class as “Child”. The parent class “Human” has a constructor setting direct values of age, height, and color. While the child class has a constructor with one extra parameter, “name”, it is calling a superclass constructor to set the values. This constructor of the child class has been setting the variable “name”. The object c1 is used to pass values in the parameter of the child class constructor. After that, four print statements have been used to print the values for all the parameters.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/17-2.png" data-lazy- height="344" src="data:image/svg xml,” width=”612″>

Let’s execute the file with the “python3” keyword and the name of a file “one.py”. Try the below query and hit the “Enter” button. The output shows four print statements defined outside of both the classes ,e.g., child and parent. There is no issue if you declare a variable in a parent class or child class.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/18-2.png" data-lazy- height="95" src="data:image/svg xml,” width=”450″>

Conclusion:

You have learned what to do to supercharge existing classes with superchargers in this tutorial. You began with a study of single inheritance, followed by a demonstration of how to invoke superclass functions with super().

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/Author-Image.jpg60cd156feca2c.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.