Class inheritance in Python (P1)
I, Introduction
Classes, along with objects, are the core components of Python. Classes provide a way to organize attributes (data) and methods (behaviors). A very important concept of OOP (Object Oriented Programming) is class inheritance. Class inheritance allows us to create a class that "inherits" all the features of one or more parent classes, modify those features, and add methods and attributes. In this article, we will explore this concept in Python.
II, How to inherit a class
The syntax to inherit a class as a parent class is as follows:
class Parent:
pass
class Child(Parent):
pass
The parent class(es) are defined in () after the child class name.
To check what the parent class(es) of a class are, we use the .__bases__ syntax. The statement will return a tuple containing the parent class(es) of . For the Child class above, the above syntax will return:
Oh so object is the parent of parents. Since Python is an OO(Object Oriented) language, everything in Python is an object except control flow. And object is the base class of all objects in Python. An example to see this is:
III. Ways to extend classes through inheritance
This is the parent class we will use in the three sections below:
1. Add new method(s):
The output will be:
2. Redefine, edit existing method(s) of parent class(es)
The output will be:
3. Add new attribute(s)
I will explain the concept of super() later. You can understand that super() in the current context is to call the previous version — that is, the Animal class. In the example above, it is to pass to the init of the Animal class two attributes, age and gender, as defined by it.
Output:
As you can see polar_bar has all two attributes of Animal which are age, gender, and fur_color attribute which is newly added to Bear class.
IV. Relationship between child and parent classes
The "inheritance" between child and base class(es) will create a "blood" relationship between the 2.
Example checking polar_bear instance type:
Therefore, a piece of code that works with an instance of a parent class should work with an instance of a child class.
V. Conclusion
In this part, I have introduced the basic properties of class - a core feature of Python. I hope this article can help you supplement your knowledge and apply it in your work. In the next part, I will talk about more advanced features of class that not all experienced Python developers know well. We look forward to your support!
You should read it
May be interested
- Introducing the greedy algorithmin this article, i will introduce a rather interesting algorithm called greedy algorithm and its detailed solution.
- Merge Linked List Problemcontinuing the linked list topic in the algorithm & data structure series, today i will introduce to you the merge linked list problem. in this article, i will give 2 solutions to the problem and analyze the space and time complexity.
- Class Inheritance in Python (Part 2)in the previous part, i mentioned the basic properties of classes in python. in this part, i will mention the advanced properties and characteristics of classes.
- Learn about the new optional chaining operator (?.) in JavaScript and its application in Vue.jsoptional chaining (?.) is a new operator introduced in es2020 of javascript. let's learn and use it in vue.js 2 and 3.
- What is ActiveMQ? Benefits of ActiveMQactivemq is an open source messaging software that serves as the backbone for the architecture of distributed applications built on messaging.
- What is NextJS? All the basic knowledge you should knownextjs is an open-source react front-end framework that offers optimization features like server-side rendering and static website generation.