python多重继承实例

yipeiwu_com5年前Python基础

本文实例讲述了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
<bound method GC.bar of <mro.GC instance at 0xb77be2ac>>
>>> gc.bar()
called P2-bar
>>>

3.结论:

方法解释顺序(MRO): 深度优先, 从左至右

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Python Queue模块详解

Python中,队列是线程间最常用的交换数据的形式。Queue模块是提供队列操作的模块,虽然简单易用,但是不小心的话,还是会出现一些意外。 创建一个“队列”对象 import Queue...

python对于requests的封装方法详解

由于requests是http类接口的核心,因此封装前考虑问题比较多: 1. 对多种接口类型的支持; 2. 连接异常时能够重连; 3. 并发处理的选择; 4. 使用方便,容易维护; 当前...

python requests 测试代理ip是否生效

代码如下所示: import requests '''代理IP地址(高匿)''' proxy = { 'http': 'http://117.85.105.170:808',...

python二进制读写及特殊码同步实现详解

python对二进制文件的操作需要使用bytes类,直接写入整数是不行的,如果试图使用f.write(123)向文件中以二进制写入123,结果提示参数不是bytes类型。 impor...

Python中顺序表的实现简单代码分享

Python中顺序表的实现简单代码分享

顺序表python版的实现(部分功能未实现) 结果展示: 代码示例: #!/usr/bin/env python # -*- coding:utf-8 -*- class Seq...