Home  >  Article  >  Backend Development  >  What is inheritance of python classes? What are the rules for class inheritance?

What is inheritance of python classes? What are the rules for class inheritance?

乌拉乌拉~
乌拉乌拉~Original
2018-08-20 14:39:123338browse

In this article, let’s learn about the inheritance of python classes. For those who have just come into contact with the programming language python, they may know less about the inheritance of python classes, but it doesn’t matter. In the next In this article, we will learn about the python class inheritance rules. I will analyze python class inheritance examples below.

Inheritance of classes

One of the main benefits of object-oriented programming is the reuse of code. One of the ways to achieve this reuse is through the inheritance mechanism.

The new class created through inheritance is called a subclass or derived class, and the inherited class is called a base class, parent class or super class.

Inheritance syntax

class 派生类名(基类名)
    ...

Some characteristics of inheritance in python:

1. If the constructor of the parent class is needed in the subclass, It is necessary to explicitly call the constructor of the parent class, or not override the constructor of the parent class. For detailed instructions, please view: python subclass inheritance parent class constructor description.

2. When calling a method of the base class, you need to add the class name prefix of the base class and bring the self parameter variable. The difference is that when calling ordinary functions in a class, you do not need to bring the self parameter

3. Python always first searches for the method of the corresponding type. If it cannot find the corresponding method in the derived class, it starts to go to the base class. Search each class one by one. (First look for the calling method in this class, and then look for it in the base class if you can't find it).

If more than one class is listed in the inheritance tuple, then it is called "multiple inheritance".

Syntax

Declaration of derived classes, similar to their parent classes, with a list of inherited base classes following the class name, as follows:

class SubClassName (ParentClass1[, ParentClass2, ...]):
    ...

Example analysis

# !/usr/bin/python
# -*- coding: UTF-8 -*-
class Parent:  # 定义父类
    parentAttr = 100
    def __init__(self):
        print "调用父类构造函数"
    def parentMethod(self):
        print '调用父类方法'
    def setAttr(self, attr):
        Parent.parentAttr = attr
    def getAttr(self):
        print "父类属性 :", Parent.parentAttr
class Child(Parent):  # 定义子类
    def __init__(self):
        print "调用子类构造方法"
    def childMethod(self):
        print '调用子类方法'
c = Child()  # 实例化子类
c.childMethod()  # 调用子类的方法
c.parentMethod()  # 调用父类方法
c.setAttr(200)  # 再次调用父类的方法 - 设置属性值
c.getAttr()  # 再次调用父类的方法 - 获取属性值

The above code execution results are as follows:

调用子类构造方法
调用子类方法
调用父类方法
父类属性 :200

You can inherit multiple classes

class A:        # 定义类 A
.....
class B:         # 定义类 B
.....
class C(A, B):   # 继承类 A 和 B
.....

You can use issubclass() Or isinstance() method to detect.

1.issubclass() - Boolean function determines whether a class is a subclass or descendant of another class, syntax: issubclass(sub,sup)

2.isinstance(obj, Class) The Boolean function returns true if obj is an instance object of the Class class or an instance object of a Class subclass.

The above is all the content of this article. This article mainly introduces the aspect of inheritance of python classes. I hope you can use the information to understand what is said above and the examples given. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.

For more related knowledge, please visit the Python tutorial column on the php Chinese website.

The above is the detailed content of What is inheritance of python classes? What are the rules for class inheritance?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn