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()
[
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: <__main__.D object at 0x000002213877F2B0> post-ping: <__main__.D object at 0x000002213877F2B0> 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: <__main__.D object at 0x000002213877F2B0>
The third call is self .pong() method, based on __mro__, finds the pong method implemented by class B. pong: <__main__.D object at 0x000002213877F2B0>
The fourth call is super().pong(), which is also based on __mro__, find the pong method implemented by class B. pong: <__main__.D object at 0x000002213877F2B0>
The fifth call is C.pong(self), __mro__ is ignored, and C is found. The pong method implemented by the class. PONG: <__main__.D object at 0x000002213877F2B0>
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!