Home  >  Article  >  Backend Development  >  A brief explanation of python inheritance and multiple inheritance (code example)

A brief explanation of python inheritance and multiple inheritance (code example)

不言
不言Original
2018-09-12 17:53:171927browse

The content of this article is a simple explanation (code example) of Python inheritance and multiple inheritance. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Remember the following points:

Directly subclassing built-in types (such as dict, list or str) is prone to errors, because methods of built-in types usually ignore user-overridden methods, do not subclass Classifying built-in types, user-defined classes should inherit the collections module.

    def __setitem__(self, key, value):
        super().__setitem__(key, [value] * 2) # 错误案例
class AnswerDict(dict):
    def __getitem__(self, item): # 错误案例
        return 42
import collections
class DoppelDict2(collections.UserDict): # 正确案例
    def __setitem__(self, key, value):
        super().__setitem__(key, [value] * 2)
class AnswerDict2(collections.UserDict): # 正确案例
    def __getitem__(self, item):
        return 42

Another problem related to multiple inheritance is: if a superclass of the same level defines an attribute with the same name. How does Python determine which one to use?

class DoppelDict(dict):
    def __setitem__(self, key, value):
        super().__setitem__(key, [value] * 2)
class AnswerDict(dict):
    def __getitem__(self, item):
        return 42
import collections
class DoppelDict2(collections.UserDict):
    def __setitem__(self, key, value):
        super().__setitem__(key, [value] * 2)
class AnswerDict2(collections.UserDict):
    def __getitem__(self, item):
        return 42
class A:
    def ping(self):
        print('Ping:', self)
class B(A):
    def pong(self):
        print('pong:', self)
class C(A):
    def pong(self):
        print('PONG:', self)
class D(B, C):
    def ping(self):
        super().ping()
        print('post-ping:', self)
    def pingpong(self):
        self.ping()
        super().ping()
        self.pong()
        super().pong()
        C.pong(self)
if __name__ == '__main__':
    d = D()
    print(d.pong()) # 输出来源于B
    print(C.pong(d)) #输出来源于C 超类的方法都可以直接调用,此时要把实例作为显示参数传入.

Python can distinguish which method is called, by Method parsing order
>>> D.mro()
[f90f74cf2b5eef2157d27f4b07137685, d3d10dde313d07caf2874a0faa31cc27, 3c8ab9bdf9f4838c4d1910062f257eb2, d65e71c32c2035ca4f3e3c46100a7a5f, 0b6b3070adce8118f880cf43f8bc8037]
If you want to delegate method calls to the super class, the recommended way is to use the built-in super( ) function.

The following is the interpretation of the d.pingpong() method

>>> self.ping()

Ping: 51c9255e984783507746d24bd1018f2c post-ping: 51c9255e984783507746d24bd1018f2c The first call is self.ping(), which runs the ping method of class D.

The second call The one is super().ping(), skip the ping method of class D and find the ping method of class A. Ping: 51c9255e984783507746d24bd1018f2c

The third call is self .pong() method, based on __mro__, finds the pong method implemented by class B. pong: 51c9255e984783507746d24bd1018f2c

The fourth call is super().pong(), which is also based on __mro__, find the pong method implemented by class B. pong: 51c9255e984783507746d24bd1018f2c

The fifth call is C.pong(self), __mro__ is ignored, and C is found. The pong method implemented by the class. PONG: 51c9255e984783507746d24bd1018f2c

Related recommendations:

Single inheritance and multiple inheritance in Python

Python classes and inheritance explanation

The above is the detailed content of A brief explanation of python inheritance and multiple inheritance (code example). 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