Python多继承顺序实例分析

yipeiwu_com5年前Python基础

本文实例讲述了Python多继承顺序。分享给大家供大家参考,具体如下:

示例1:

#-*- coding:utf-8 -*-
#!python2
class A(object):
  def caller(self):
    print 'A caller'
    self.called()
  def called(self):
    print 'A called'
class B(object):
  def called(self):
    print 'B called'
class C(B,A):
  pass
if __name__ == '__main__':
  c=C()
  c.caller()

运行结果:

A caller
B  called

示例2:

#-*- coding:utf-8 -*-
#!python2
class A(object):
  def caller(self):
    print 'A caller'
    self.called()
  def called(self):
    print 'A called'
class B(object):
  def called(self):
    print 'B called'
class C(A,B):
  pass
if __name__ == '__main__':
  c=C()
  c.caller()

运行结果:

A caller
A called

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程

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

相关文章

Python实现读取文件最后n行的方法

本文实例讲述了Python实现读取文件最后n行的方法。分享给大家供大家参考,具体如下: # -*- coding:utf8-*- import os import time impo...

python正则表达式re模块详细介绍

本模块提供了和Perl里的正则表达式类似的功能,不关是正则表达式本身还是被搜索的字符串,都可以是Unicode字符,这点不用担心,python会处理地和Ascii字符一样漂亮。 正则表...

使用python绘制人人网好友关系图示例

代码依赖:networkx matplotlib 复制代码 代码如下: #! /bin/env python# -*- coding: utf-8 -*-import urll...

python简单程序读取串口信息的方法

本文实例讲述了python简单程序读取串口信息的方法。分享给大家供大家参考。具体分析如下: 这段代码需要调用serial模块,通过while循环不断读取串口数据 import tim...

python 装饰器功能以及函数参数使用介绍

python 装饰器功能以及函数参数使用介绍

简单的说:装饰器主要作用就是对函数进行一些修饰,它的出现是在引入类方法和静态方法的时候为了定义静态方法出现的。例如为了把foo()函数声明成一个静态函数 复制代码 代码如下: class...