Python

Python Super Function Usage

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.

$ python3 --version

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

$ sudo apt update

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

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

Press the Enter key to proceed:

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

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

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.

$ pip --version

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

$ sudo apt install curl

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

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

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

$ ls get*

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

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

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

$ pip3.9 --version

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”:

$ touch one.py

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.

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.

$ python3 one.py

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.

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.

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

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.