本文实例讲述了python多重继承用法,分享给大家供大家参考。具体实现方法如下:
1.mro.py文件如下:
#!/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.执行结果如下:
>>> from mro import *
>>> gc = gc()
>>> gc.foo()
called p1-foo
>>> gc.bar
>>> gc.bar()
called p2-bar
>>>
3.结论:
方法解释顺序(mro): 深度优先, 从左至右
希望本文所述对大家的python程序设计有所帮助。