Python

Python Advanced Classes

Python Advanced Classes

This page covers Python advanced classes and continues the previous tutorial of Python intermediate classes.

For Python intermediate classes here.

It encompasses some of the aspects of classes in more details.

  • class inheritance
  • “super()” function
  • multiple inheritance

Class inheritance

As the name suggests, inheritance refers to passing something from one thing to another.

With classes in Python, inheritance allows one class to inherit the properties as well as methods of another class.

The terms referring to both classes are parent class and child class. And just like in real life, the child inherits from the parent. Therefore, a child class in Python can get properties and/or methods from a parent class.

There are many scenarios where inheritance implementation can apply. For instance:

  • dog: Poodle, Rottweiler, and Husky
  • car: BMW, Mercedes, and Tesla
  • animal: Dog, Bird, and Rabbit
  • employee: Brian, William, and Peter
  • shape: Circle, Square, and Triangle
  • vehicle: Bus, Car, and Truck

Parent class

A parent class is just a normal class, as defined previously. Therefore, any initial class refers to a parent class.

For this reason, there are no changes to the syntax of a parent class.

The following is a parent class, continuing the “car” example from the basics and intermediate classes tutorials.

This instance dives further, by implementing a parent class (vehicle).

class Vehicle:
    def __init__(self, vehicle, wheels, size):
        self.vehicle = vehicle
        self.wheels = wheels
        self.size = size

The above defines what a vehicle is (in this scenario). A vehicle can be anything (e.g., motorcycle, bus, car, etc.). This is where a child class comes into play.

Child class

A child class is a sub-class of the parent class. And as such, can inherit properties and methods from a parent class.

Please note that a child class can have properties and methods on its own (in addition to the parents’), as well as overwrite anything from the parent class.

The implementation of parent/child classes is essentially a hierarchical system.

The initialisation of a child class operates the same way as a parent class, with the exception of inputting the parent class name in its argument section.

Below is an example.

class Car(Vehicle):
    pass

As previously mentioned, classes cannot be empty. Therefore, when the child class does not add anything else apart from the parent’s class, the “pass” statement applies.

Let’s create two objects, one for each class.

parent = Vehicle("Car", "4", "Medium")
child = Car("Car", "4", "Medium")

print(parent.wheels)
print(child.size)

# Output: 4
# Output: Medium

Even though the child class is empty (only a pass statement), it still has all properties of the parent class.

Let’s add separate (own) properties to the child class.

Please note that adding properties to a child class happens through the “__init__()” function. Consequently, the child class properties overwrite the parent class.

In order to include the parent class properties, Python requires two things:

  • include parent properties inside the “__init__()” function of the child class
  • call parent class “__init__()” function inside the child class

See below.

class Car(Vehicle):
    def __init__(self, vehicle, wheels, size, brand, model, kind, colour):
        Vehicle.__init__(self, vehicle, wheels, size)
        self.brand = brand
        self.model = model
        self.kind = kind
        self.colour = colour

There are four new properties the child class has: brand, model, kind, and colour. But it still owns the parent class properties (vehicle, wheels, and size).

These three parent class properties go inside the child class initialisation function. In addition, the child class calls the parent class initialisation function.

Now we create new and more-detailed car objects, with all properties.

child2 = Car("Car", "4", "medium", "BMW", "X1 2024", "Coupe", "Dark Navy")

print(child2.model)
print(child2.wheels)

# Output: X1 2024
# Output: 4

super() function

The function “super()” helps a child class obtain all methods and properties of the parent class.

In simple usage, the “super()” function works the same way as calling the “__init__()” function of the parent class (see above). The difference is that, Python removes the need to call explicitly the parent class function.

The difference in syntax is as follows:

# Calling parent class function
class Car(Vehicle):
    def __init__(self, vehicle, wheels, size):
        Vehicle.__init__(self, vehicle, wheels, size)

# Using super() function
class Car(Vehicle):
    def __init__(self, vehicle, wheels, size):
        super().__init__(vehicle, wheels, size)

Please note that the “self” parameter inside the “super()” function is not present.

The full example.

class Vehicle:
    def __init__(self, vehicle, wheels, size):
        self.vehicle = vehicle
        self.wheels = wheels
        self.size = size

class Car(Vehicle):
    def __init__(self, vehicle, wheels, size):
        super().__init__(vehicle, wheels, size)

parent = Vehicle("Car", "4", "Medium")
child = Car("Car", "4", "Medium")
child2 = Car("Car", "4", "Medium")

print(parent.wheels)
print(child.size)
print(child2.vehicle)
# Output:
4
Medium
Car

Multiple inheritance

The main advantage of the “super()” function is with multiple inheritances.

Below is an example of multiple inheritance.

class Vehicle:
    def __init__(self, vehicle, wheels, size):
        self.vehicle = vehicle
        self.wheels = wheels
        self.size = size

class Car(Vehicle):
    def __init__(self, vehicle, wheels, size):
        print("This is a car.")
        super().__init__(vehicle, wheels, size)

class Coupe(Car):
    def __init__(self, vehicle, wheels, size):
        print("This is a coupe car.")
        super().__init__(vehicle, wheels, size)

class Sports(Car):
    def __init__(self, vehicle, wheels, size):
        print("This is a sports car.")
        super().__init__(vehicle, wheels, size)

class SUV(Sports, Coupe):
    def __init__(self, vehicle, wheels, size):
        print("This is a SUV.")
        super().__init__(vehicle, wheels, size)

Let’s create a SUV object.

suv = SUV("SUV", "4", "Medium-Large")
# Output:
This is a SUV.
This is a sports car.
This is a coupe car.
This is a car.
print(suv.size)

# Output: Medium-Large

The flow of operation follows the method resolution order (MRO). The MRO refers to the order of inheritance.

The above example operates as follows:

  1. Python creates an object from the SUV sub-class.
  2. The SUV class executes, but it also refers to the Sports and Coupe classes, respectively.
  3. The Sports class executes, followed by the Coupe class.
  4. The references continues by calling the Car class.
  5. Car class refers to the parent Vehicle class, and as such assigns all relevant properties.

Next: Python Advanced Files

by AICorr Team

We are proud to offer our extensive knowledge to you, for free. The AICorr Team puts a lot of effort in researching, testing, and writing the content within the platform (aicorr.com). We hope that you learn and progress forward.