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
- Storage class in C / C ++the storage class defines the scope and life of variables and / or functions within a c / c ++ program. they often precede the type of data they impact.
- Classmethod () function in Pythonin python, the classmethod () function returns a class method for the function. how is the syntax of classmethod () function, what parameters does it have and how to use it? invites you to read the track.
- Bookmark 5 best Python programming learning websitesif you are a developer or you are studying and want to stick with this industry, learn python to add a highlight in your journey.
- For in Python loopin this article, we will learn more about for for loop in python as well as its variations, how to use for to repeat a string of elements in python such as list, string or other iterative objects.
- Manage files and folders in Pythonpython also provides a variety of methods to handle various directory-related operations. in this article, we will learn about managing files and directories in python, namely creating folders, renaming folders, listing folders and working with them.
- Multiple choice quiz about Python - Part 3today's topic quantrimang wants to challenge you is about file and exception handling in python. let's try the following 15 questions!
- 5 choose the best Python IDE for youin order to learn well python, it is essential that you find yourself an appropriate ide to develop. quantrimang would like to introduce some of the best environments to help improve your productivity.
- Class selector in CSSin css, class is used to style the element with the specified class.
- What is Python? Why choose Python?python is a powerful, high-level, object-oriented programming language, created by guido van rossum. python is easy to learn and emerging as one of the best introductory programming languages for people who are first exposed to programming languages.
- Module time in Pythonpython has a time module used to handle time-related tasks. tipsmake.com will work with you to find out the details and functions related to the time specified in this module. let's follow it!