“This is the 15th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Official Python column # 38, stop! Don’t miss this zero-based article!

The previous installment showed and shared the structure of object-oriented programming and classes, and ended with a little mention of inheritance.

This time we will explain the terms inheritance and inheritance derivation together

Python supports single inheritance and multiple inheritance

Taking advantage of the deep impression, the student committee took the code of the previous article and slightly modified it:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @time: 2021/11/15 11:58 PM
# @Author : LeiXueWei
# @csDN /Juejin/Wechat: Lei Xuewei
# @XueWeiTag: CodingDemo
# @File : __init__.py.py
# @Project : hello

""" Here is a programmer class definition.


class Programmer() :
    def code(self) :
        print("life is short, why not python?")

""" Here is a student class definition """


class Student(object) :
    """ Here is a student class definition """

    def __init__(self, name) :
        self.name = name

    def get_name(self) :
        return self.name

    def set_name(self, name) :
        self.name = name

    def study(self) :
        print(f"{self.name}Good good study, day day up!")


class PrimarySchoolStudent(Student, Programmer) :
    pass


print("*" * 16)
xiaopengyou = PrimarySchoolStudent("A pupil (a fan of the committee)")
xiaopengyou.study()
xiaopengyou.code()

print("Base class of class :", PrimarySchoolStudent.__bases__)
Copy the code

Here is the result:

As you can see, it is very easy to create a new class, PrimarySchoolStudent, with two class behaviors and properties.

Python provides the utility function ‘issubClass’ to identify the relationship between a class and a class.

‘isinstance’ also helps us identify the relationship between an object and a class.

Print ("PrimarySchoolStudent is a Programmer?" , isSubClass (PrimarySchoolStudent, Programmer) print("PrimarySchoolStudent, Programmer) , isSubClass (PrimarySchoolStudent, Student)) print("Student is a subclass of Programmer?" If ("xiaopengyou is an instance of PrimarySchoolStudent? ") ", isinstance(xiaopengyou, PrimarySchoolStudent)) print(" Xiaopengyou, Programmer ") ", isinstance(xiaopengyou, Programmer) print(" Xiaopengyou, Programmer) ", isinstance(xiaopengyou, Student)) print(" Is xiaopengyou dict instance?" , isinstance(xiaopengyou, dict))Copy the code

Try to copy and run this code to impress the reader.

Sometimes sons don’t want to know what their fathers know, and subclasses can override — Tknow

For example, the pupil class wants to rewrite the study function to play a game.

In object-oriented programming, this is allowed and legal! (Unless the final property is set in Java, which is outside the scope of this article)

So, LET me modify it:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @time: 2021/11/15 11:58 PM
# @Author : LeiXueWei
# @csDN /Juejin/Wechat: Lei Xuewei
# @XueWeiTag: CodingDemo
# @File : __init__.py.py
# @Project : hello
""" Here is a programmer class definition.


class Programmer() :
    def code(self) :
        print("life is short, why not python?")


""" Here is a student class definition """


class Student(object) :
    """ Here is a student class definition """

    def __init__(self, name) :
        self.name = name

    def get_name(self) :
        return self.name

    def set_name(self, name) :
        self.name = name

    def study(self) :
        print(f"{self.name}Good good study, day day up!")


class PrimarySchoolStudent(Student, Programmer) :
    def study(self) :
        print(f"{self.name}: Have fun playing the game!")


print("*" * 16)
xiaopengyou = PrimarySchoolStudent("A pupil (a fan of the committee)")
xiaopengyou.study() 
xiaopengyou.code()
Copy the code

Again, inheritance gives subclasses direct access to the functions and data properties of their parent classes.

However, this change is just one more function than the previous code, the student committee in PrimarySchoolStudent, redefine a study function.

So the subclass’s function overwrites the parent class’s function. Note that you must say functions of the same name!

Ok, look at the following results:

conclusion

Class inheritance produces parent-child inheritance, but allows differences between parent and child. That’s all for today.

The committee has been writing Java for more than a decade, but this Python tutorial is very pragmatic, so if you have any questions about basic programming, please see the related article.

For those who like Python, please check out the Python Basics section or the Python Getting Started to Master Section

Continuous learning and continuous development, I am Lei Xuewei! Programming is fun. The key is to get the technology right. Welcome to wechat, like support collection!