Home  >  Article  >  Backend Development  >  In-depth understanding of multiple inheritance in python

In-depth understanding of multiple inheritance in python

黄舟
黄舟Original
2017-08-07 15:37:181375browse

This article mainly introduces relevant information on the understanding of multiple inheritance in Python. Multiple inheritance is not easy to understand. Here are examples to help you learn and reference. Friends who need it can refer to it

python Understanding of multiple inheritance

Python, like C++, supports multiple inheritance. Although the concept is easy, the difficult task is if a subclass calls an attribute that is not defined by itself, in what order does it go to the parent class to find it, especially when many of the parent classes contain the attribute with the same name.

The search order of attributes is different for classic classes and new-style classes. Now let’s take a look at the two different performances of the classic class and the new class respectively:

Classic class:


#! /usr/bin/python
# -*- coding:utf-8 -*-

class P1():
 def foo(self):
  print 'p1-foo'

class P2():
 def foo(self):
  print 'p2-foo'
 def bar(self):
  print 'p2-bar'

class C1(P1,P2):
 pass

class C2(P1,P2):
 def bar(self):
  print 'C2-bar'

class D(C1,C2):
 pass


if __name__ =='__main__':
 d=D()
 d.foo()
 d.bar()

Execution results :


p1-foo
p2-bar

Draw a picture of the code example for easy understanding:

From the output of the above classic class From the results,

When instance d calls foo(), the search order is D => C1 => P1,

When instance d calls bar(), the search order is D = > C1 => P1 => P2

Summary: The classic search method is to find attributes according to the "left to right, depth first" method. d first searches to see if it has a foo method. If not, it searches to see if the method exists in the nearest parent class C1. If not, it continues to search upward until the method is found in P1, and the search ends.

New style class:


#! /usr/bin/python
# -*- coding:utf-8 -*-

class P1(object):
 def foo(self):
  print 'p1-foo'
  
class P2(object):
 def foo(self):
  print 'p2-foo'
 def bar(self):
  print 'p2-bar'
  
class C1(P1,P2):
 pass
 
class C2(P1,P2):
 def bar(self):
  print 'C2-bar'
  
class D(C1,C2):
 pass 
 

if __name__ =='__main__':
 print D.__mro__ #只有新式类有__mro__属性,告诉查找顺序是怎样的
 d=D()
 d.foo()
 d.bar()

Result of execution:


(<class &#39;__main__.D&#39;>, <class &#39;__main__.C1&#39;>, <class &#39;__main__.C2&#39;>, <class &#39;__main__.P1&#39;>, <class &#39;__main__.P2&#39;>, <type &#39;object&#39;>)

p1-foo
C2-bar

Judging from the output results of the new style class above,

When instance d calls foo(), the search order is D => C1 => C2 => P1

When instance d calls bar(), the search order is D => C1 => C2

Summary: The search method of new-style classes is to use the "breadth-first" method to find attributes.

The above is the detailed content of In-depth understanding of multiple inheritance in python. 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