Python Extend Class: Leveraging Inheritance for Code Reusability

Photo of author

We can create a new class in Python by extending a current one. Because Python offers the inheritance feature, this is made possible. We’ll describe the Python extend class in this article.

In Python, a function is an extended method, and the process is known as extending when a subclass defines a function that exists in its superclass to add extra functionality in its special style. 

For additional details on the Python extension class, continue reading.

Python Extend Class: Why extend a function?

Inheritance in Python allows for the extension of class methods. One of the main benefits of extending a technique is that it makes the code reusable.

extend function i n python

Additionally, several subclasses can use the same code and even update it as necessary.

See also: Templating In Python: Understanding Template Class In Python

Simply put, when we extend a class, we mean that we want to derive a new class—a child class—from an already-existing class. It is the same to extend a class as to inherit one.

Let’s look at how a Python class extends another class using inheritance.

Python Extend Class using Inheritance

In Python, we provide the name of the parent class as a parameter when constructing the child class to inherit it.

python inheritance

Syntax:


class ChildClass(ParentClass)

Let’s use an example to grasp this better.

The parent class Desserts, which has two methods, init, and intro, is what we first create. A print statement in the method introduction prints the flavor and color displayed in the output.


class Desserts:

def __init__(self, flavor, color):

self.flavor = flavor

self.color = color

def intro(self):

print(self.flavor, self.color)

obj = Desserts("Vanilla", "Pink")

obj.intro()

Output: Vanilla Pink

We construct the Cake Child class, which will inherit from the Desserts class. The name of the Desserts class will be provided as a parameter when creating the Cake class for it to derive from the Desserts class.


class Cake(Desserts):

pass

In this situation, we use the pass keyword because we only inherit the Desserts class from the Cake class and do not offer any new features or methods. The dessert class now has the same properties and techniques as the cake class.

We can verify this by creating an instance of the Cake class and executing the intro method, as demonstrated in those final two lines of code.


class Desserts:

def __init__(self, flavor, color):

self.flavor = flavor

self.color = color

def intro(self):

print(self.flavor, self.color)

obj = Desserts("Vanilla", "Pink")

obj.intro()

class Cake(Desserts):

pass

obj = Cake("Black forest", "Black")

obj.intro()

Output:

Vanilla Pink

Black Forest Black

Notice how the Cake class calls the intro function like the Desserts class. Let’s examine how we might expand this newly established child class’s methods and attributes.

Read also: Python In Research: Python For Scientific Computing

After the Python extending class, the child class should include the init () function

Even when a class is inherited, you can add a new init() function to the child class. Remember that the init() function is always called when establishing a new class object.

Python extend class-init function

Additionally, if the child class includes the init() function, the parent class’s init() procedure won’t be used.


class Cake(Desserts):

def __init__(self, flavor, color):

self.flavor = flavor

self.color = color

Even though the child class’s init() method overrides the parent class’s init() method’s inheritance, we may still use the parent’s init() method by calling it as follows:


def __init__(self, flavor, color):

Desserts.__init__(self, flavor, color)

Make use of the super() function after Python extends the class

In Python, use the super() function to get the properties and methods of a parent class.

Python extend class-super fn

We can utilize the super() function to inherit all the methods and attributes from the parent class whenever there is a new init() within a child class that is using the parent’s init() method.


class Cake(Desserts):

def __init__(self, flavor, color):

super().__init__(flavor, color)

Note that the super() function automatically recognizes the parent class. In this case, we are not supplying its name. The foundational concepts of Python inheritance were at the center of everything. But what if we need to give the child class new characteristics or methods? See if we can accomplish it.

Check this out: Object-Oriented Python: Understanding Constructors In Python

After Python extends the class, add attributes to the child class

Similar to adding any other attribute, we can add additional attributes to a child class in addition to those that are inherited from the parent class. See how the Cake class has a quantity attribute added.


class Cake(Desserts):

def __init__(self, flavor, color, quantity):

super().__init__(flavor, color)

self.quantity = quantity

The init() function of the child class now has a second parameter. Don’t forget to include one extra quantity value when constructing an object of the Cake class, as seen here:


obj = Cake("Black forest", "Black", 5)

After the Python class extends, add methods to the child class

Using the def keyword, we add a method, i.e., price, to the code below. Additionally, we supply the self keyword as a parameter.


class Cake(Desserts):

def __init__(self, flavor, color, quantity):

super().__init__(flavor, color)

self.quantity = quantity

def price(self):

print("Pay for: ", self.quantity, "items.")

The complete code would look like this:


class Desserts:

def __init__(self, flavor, color):

self.flavor = flavor

self.color = color

def intro(self):

print(self.flavor, self.color)

obj = Desserts("Vanilla", "Pink")

obj.intro()

class Cake(Desserts):

def __init__(self, flavor, color, quantity):

super().__init__(flavor, color)

self.quantity = quantity

def price(self):

print("Pay for: ", self.quantity, "items")

obj = Cake("Black forest", "Black", 5)

obj.intro()

obj.price()

Output:

Vanilla Pink

Black Forest Black

Pay for: 5 items

The inherited method will be overridden if you add a method with the same name to any parent class. In Python, we can extend a class in this manner.

See also: How To Import A Class From Another File In Python?

Adding vs. Extend List Methods in Python

You can add elements to a list with the extend() and append() functions. These two approaches, however, accomplish quite different goals. The append() function adds one entry to the end of a list. We extend() a list to include more than one element.

Python extend class-adding vs extend

The provided element is added as one item at the end of the original list using the append() function. Bypassing an Iterable as a parameter, the extend() method appends each element to the list individually. It modifies the original list.

Read also: Python Basics: Understanding Identifiers In Python

FAQs

What does Python extension mean?

Writing Python modules in C – C extension modules — is called 'Python extension' in the Python documentation.

Is inheritance supported in Python?

Python does indeed support inheritance. We can create a child class with the parent class's features and functions via inheritance. In addition to the features already present in the parent class, we may add new features to the child class.

What is the list extended in Python?

The extends() method of the Python List iterates over an iterable (such as a string, list, tuple, etc.) and modifies the original list by adding each element of the iterable to the end of the Python List.

When one class extends another, what happens?

Any attributes or characteristics of a class that extends it are also inherited. Additionally, by including the override keyword in the method declaration, the extending class can replace the already-existing virtual methods.

Conclusion

We hope that this explanation of the Python extend class proved helpful. It is a method for Python to demonstrate polymorphism. Overriding in Java and virtual methods in C++ are analogous to this.

The subclass then uses the function after calling it from the subclass instance. When calling the superclass version, one uses the Super() function.

Python also provides multiple approaches to check for file existence, each suited for different scenarios and requirements

Check this out: Visualizing Code Logic: Python Flowchart Symbols

Leave a Comment