Python Multiple inheritance sequence example analysis

  • 2020-09-16 07:36:38
  • OfStack

This article gives an example of Python multiple inheritance order. To share for your reference, the details are as follows:

Example 1:


#-*- coding:utf-8 -*-
#!python2
class A(object):
  def caller(self):
    print 'A caller'
    self.called()
  def called(self):
    print 'A called'
class B(object):
  def called(self):
    print 'B called'
class C(B,A):
  pass
if __name__ == '__main__':
  c=C()
  c.caller()

Operation results:

[

A caller
B called

]

Example 2:


#-*- coding:utf-8 -*-
#!python2
class A(object):
  def caller(self):
    print 'A caller'
    self.called()
  def called(self):
    print 'A called'
class B(object):
  def called(self):
    print 'B called'
class C(A,B):
  pass
if __name__ == '__main__':
  c=C()
  c.caller()

Operation results:

[

A caller
A called

]

More about Python related content interested readers to view this site project: introduction to Python object-oriented programming and advanced tutorial ", "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using techniques", "Python string skills summary", "Python coding skills summary" and "Python introductory and advanced tutorial"

I hope this article has been helpful for Python programming.


Related articles: