Python multiple inheritance instances
- 2020-04-02 14:13:54
- OfStack
This article is an example of how to use python multiple inheritance. The specific implementation method is as follows:
1. The mro.py file is as follows:
#!/usr/bin/python
# Filename:mro.py
class P1:
def foo(self):
print 'called P1-foo'
class P2:
def foo(self):
print 'called P2-foo'
def bar(self):
print 'called P2-bar'
class C1(P1, P2):
pass
class C2(P1, P2):
def bar(self):
print 'called C2-bar()'
class GC(C1, C2):
pass
2. The implementation results are as follows:
>>> from mro import *
>>> gc = GC()
>>> gc.foo()
called P1-foo
>>> gc.bar
<bound method GC.bar of <mro.GC instance at 0xb77be2ac>>
>>> gc.bar()
called P2-bar
>>>
3. Conclusion:
Method interpretation order (MRO) : depth-first, from left to right
I hope this article has helped you with your Python programming.