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:type、object、class与内置类型实例

Python:type、object、class Python: 一切为对象 >>> a = 1 >>> type(a) <class'in...

python gdal安装与简单使用

python gdal安装与简单使用

gdal安装 方式一:在网址 https://www.lfd.uci.edu/~gohlke/pythonlibs/ 下载对应python版本的whl文件,在命令行中pip instal...

Python基于PycURL实现POST的方法

本文实例讲述了Python基于PycURL实现POST的方法。分享给大家供大家参考。具体如下: import pycurl import StringIO import urllib...

Python SQLite3简介

最近需要用Python写一个简易通讯录,但是对于数据存储很发愁。大家都知道,使用 Python 中的列表和字典进行存储数据是很不靠谱的,所以就想到Python有没有内置的数据库模块。 S...

Python 转义字符详细介绍

Python 转义字符 在需要在字符中使用特殊字符时,python用反斜杠(\)转义字符。如下表: 原始字符串 有时我们并不想让转义字符生效,我们只想显示字符串原来的意思,这就要用r和...