This is a text field

Avoid the building of python classes from scratch!

Image credit: Python in Plain English

Recently, I have to create many classes in python. It taking too much time to create the class from scratch.

So, I have to found something more interesting. Something which saves me time and energy.

So, I have moved toward python inheritance.

In python inheritance, you can create a new class from the parent class.

You can add new functionality to the child class and change the feature you don’t want in python child class.

It saves your time and energy. You can bundle the data and functionality in the python class with inheritance.

So, the next question is what you need to create an inheritance?

You need two classes to inheritance:-

• Parent class
• Child class

So, In this blog, you are going to see something interesting.

Let’s move forward to that.


Parent class

The parent class is a class from where all properties are inherited.

It is a blueprint of another class.

For example,

Output:-

In the above class student, I have created a parent class.

You can see that there is also a method name that adds functionality to the parent class.

All properties of the child class come from the parent class.


Child class

The child class is a new class which you want to create from the parent class.

That means all the properties of the parent class inherited to this class.

In the above code, the parent class is a student.

All the properties of the student class are going to the below class.

For example,

Child class

In the above example, you can see that I have created a child class person.

You can also see that there is a pass statement. That means I have not to add more properties to the class person.

Call the child class

Let’s see what happen when we call the child class.

For example,

Output:

In the above example, you can see that when I have called child class the result is the same as student class which is the parent class.

So, what the code will look like when the parent and child classes combine.

For example,

Parent and child class

Output:

Child class output

In the above example, you can see that how I had combined both parent and child classes using inheritance.

Next, you are going to see if you have to add methods and properties in the child class.

How will you do it?


Add the __init__ function in the child class

In this section, you are going to see how to add the __init__ function in the child class.

You can initialize the attribute in the child class.

For example,

In the above example, you can see that I have added the __init__ method in the child class person.

The __init__ method in the child class initializes the attributes from the child class.

But we have also need Initialization from the parent class. So, I have to use student.__init__(self,fname, lname).


Super() function in child class

The super() function in python inherited all the properties and methods from the parent class to a child class.

For example,

Output:

In the above example, you can see that the super() method has inherited all the properties and methods from the parent class student.

Here, you can see that you have not use the parent class name to inherit properties.

The super() method automatically inherited all properties from the parent class.


Add properties in child classes

You can add the properties in the child classes. For example, you have to add the person age in the child class.

For example,

Output:

In the above example, you can see that I have added an attribute age in a class person which is a child class.

You can also pass new attribute to parameters.

For example,

Added attribute in the child __init__

Output:

In the above example, you can see that I have added a new attribute age which is passing through a class person __init__ function.

In this case, you can pass any age you want.


Add a method to the child class

You can add a method to the child class and display it in output.

For example,

After calling the child class how would the output be seen?

For example,

Output:

In the above example, you can see that how I have called the method in the child class person.

You can add any method in the child class.


Conclusion:

With the help of the inheritance, you don’t need to create a class from scratch.

You only need parent class which work as a blueprint for the child class.

You can inherit the properties from the parent class.

You can also add new attributes and methods in child class.

These two properties taking from old and building new attributes and methods save the time and energy of the people.



I hope you loved this blog. If you found something misinformed you can comment on it.

For more, you can


"Quality brings clarity"

Avatar for Vipul kunwar
Follow me

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Back to top