python多重继承实例

yipeiwu_com6年前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 数字类型和字符串类型的相互转换实例

一、python中字符串转换成数字 (方法1) 类中进行导入:import string str='555' num=string.atoi(str) num即为str转换成的数字...

python遍历数组的方法小结

本文实例总结了python遍历数组的方法。分享给大家供大家参考。具体分析如下: 下面介绍两种遍历数组的方法,一种是直接通过for in 遍历数组,另外一种是通过rang函数先获得数组长度...

解决win64 Python下安装PIL出错问题(图解)

解决win64 Python下安装PIL出错问题(图解)

1、软件版本 首先我先安装了 python 2.7 pip是  8.1.2 2、当我要安装PIL时,我在cmd下面输入:pip install PIL 错误提示是: Coul...

使用Python的Django框架实现事务交易管理的教程

 如果你花费了很多的时间去进行Django数据库事务处理的话,你将会了解到这是让人晕头转向的。 在过去,只是提供了简单的基础文档,要想清楚知道它是怎么使用的,还必须要通过创建和...

解决python 无法加载downsample模型的问题

downsample 在最新版本里面修改了位置 from theano.tensor.single import downsample (旧版本) 上面以上的的import会有error...