普通继承

  1. class FooParent(object):
  2. def __init__(self):
  3. self.parent = 'I\'m the parent.'
  4. print 'Parent'
  5. def bar(self, message):
  6. print message, 'from Parent'
  7. class FooChild(FooParent):
  8. def __init__(self):
  9. FooParent.__init__(self)
  10. print 'Child'
  11. def bar(self, message):
  12. FooParent.bar(self, message)
  13. print 'Child bar function.'
  14. print self.parent
  15. if __name__ == '__main__':
  16. foochild = FooChild()
  17. foochild.bar('Hello World!')
  1. # output
  2. Parent
  3. Child
  4. Hello World! from Parent
  5. Child bar function.
  6. I'm the parent.

super继承

  1. class FooParent(object):
  2. def __init__(self):
  3. self.parent = 'I\'m the parent.'
  4. print 'Parent'
  5. def bar(self, message):
  6. print message, 'from Parent'
  7. class FooChild(FooParent):
  8. def __init__(self):
  9. super(FooChild, self).__init__()
  10. print 'Child'
  11. def bar(self, message):
  12. super(FooChild, self).bar(message)
  13. print 'Child bar function.'
  14. print self.parent
  15. if __name__ == '__main__':
  16. foochild = FooChild()
  17. foochild.bar('Hello World!')
  1. # output
  2. Parent
  3. Child
  4. Hello World! from Parent
  5. Child bar function.
  6. I'm the parent.

python super用法的更多相关文章

  1. [python] super() 用法

    问题的发现与提出 在Python类的方法(method)中,要调用父类的某个方法,在Python 2.2以前,通常的写法如下: class A: def __init__(self): print & ...

  2. Python中的super()用法

    Python中对象方法的定义很怪异,第一个参数一般都命名为self(相当于其它语言的this,比如:C#),用于传递对象本身,而在调用的时候则不 必显式传递,系统会自动传递. 今天我们介绍的主角是su ...

  3. python super()函数

    super()函数是用来调用父类(超类)的一个方法 super()的语法: python 2 的用法: super(Class, self).xxx  # class是子类的名称 class A(ob ...

  4. Python ---- super()使用

    Python ---- super() 我们经常在类的继承当中使用super(), 来调用父类中的方法.例如下面: 1 2 3 4 5 6 7 8 9 10 11 12 13 class A:     ...

  5. Python高级用法总结

    Python很棒,它有很多高级用法值得细细思索,学习使用.本文将根据日常使用,总结介绍Python的一组高级特性,包括:列表推导式.迭代器和生成器.装饰器. 列表推导(list comprehensi ...

  6. python argparse用法总结

    转:python argparse用法总结 1. argparse介绍 argparse是python的一个命令行解析包,非常适合用来编写可读性非常好的程序. 2. 基本用法 prog.py是我在li ...

  7. Anaconda下载及安装及查看安装的Python库用法

    Anaconda下载及安装及查看安装的Python库用法 Anaconda 是一个用于科学计算的 Python 发行版,提供了包管理与环境管理的功能.Anaconda 利用 conda 来进行 pac ...

  8. python enumerate用法总结【转】

    enumerate()说明 enumerate()是python的内置函数 enumerate在字典上是枚举.列举的意思 对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enum ...

  9. this和super用法

    1. this能分清混淆,形参名与当前对象的某个成员有相同的名字,需要明确使用this关键字来指明你要使用某个成员,使用方法是“this.成员名”. 一般以this.形参数名=形参名,代表送进来赋值的 ...

随机推荐

  1. Entity Framework Code-First(10.3):Property Mappings

    Property Mappings using Fluent API: Here, we will learn how to configure properties of an entity cla ...

  2. hdu1069

    #include <iostream> #include <algorithm> #include <cstring> using namespace std; c ...

  3. ubuntu14编译SSF(ethzasl_sensor_fusion )

    参考:http://wiki.ros.org/ethzasl_sensor_fusion 1. cd catkin_ws/src/ 2 git clone git://github.com/ethz- ...

  4. UITableViewCell 的复用机制

    cell重用机制 http://blog.cnrainbird.com/index.php/2012/03/20/guan_yu_uitableview_de_cell_fu_yong_tan_tan ...

  5. MongoDB自定义存储数据库文件位置

    mongodb下载地址:https://www.mongodb.com/download-center#community 本机安装目录如下: 配置步骤如下: 1.新建文件夹data(文件夹内再建一个 ...

  6. CacheManager 概述

    1. CacheManager 管理缓存,而缓存可以是基于内存的缓存,也可以是基于磁盘的缓存 2. CacheManager 需要通过 BlockMananger 来操作数据: 3. 当 Task 运 ...

  7. 消耗战——dp+虚树

    题目 [题目描述] 在一场战争中,战场由 $n$ 个岛屿和 $n-1$ 个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为 $1$ 的岛屿,而且他们已经没有足够多 ...

  8. thinkphp5.1控制器初始化函数initialize与构造函数__construct区别

    构造函数中子类的构造方法会覆盖父类的构造方法,如果要继承父类的构造方法可以加入parent::__construct(); 例子: //另一种方法,使用构造函数初始化 public function ...

  9. iOS无线真机调试

    打开xcode,选择Window > Devices and Simulators 用数据线连接设备 选择 Connect via network

  10. jmeter 签名MD5生成(转)

    请求接口需要同时发送签名,签名定义为: 可以看出签名就是把用户的密码 .用户名 和签名key生成一个md5串就可以了 刚好jmeter 有个md5 生成,生成前需要获取name ,password k ...