python super()函数
super()函数是用来调用父类(超类)的一个方法
super()的语法:
python 2 的用法:
super(Class, self).xxx # class是子类的名称
class A(object):
pass class B(a):
def add(self, x):
super(B,self).add(x)
python 3用法:
super().xxx
class A:
pass class B(A):
def add(self, x):
super().add(x) 实例:
# _*_ coding:utf-8 _*_ class FooParent:
def __init__(self):
self.parent = 'I\'m the parent'
print('parent') def bar(self, message):
print('%s from Parent' % message) class FooChild(FooParent):
def __init__(self):
super(FooChild, self).__init__() # 把FooChild对象转化成FooParent的对象,调用父类里的__init__(self),会输出‘parent'
self.child = 'this is child'
print('child') # 上一步调用结束后,打印‘child' def bar(self, message):
super(FooChild, self).bar(message) # 调用父类里的bar函数
print('child bar function')
print(self.parent) # 打印父类的属性parent
print(self.child)
if __name__ == '__main__':
fooChild = FooChild()
fooChild.bar('HelloWorld') 输出结果
parent
child
HelloWorld from Parent
child bar function
I'm the parent
this is child
python super()函数的更多相关文章
- Python super() 函数的概念和例子
概念: super() 函数是用于调用父类(超类)的一个方法. super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO).重 ...
- Python super() 函数
super() 函数是用于调用父类(超类)的一个方法. super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果重定义某个方法,该方法会覆盖父类的同名方法,但有时 ...
- python super()函数详解
引言: 在类的多继承使用场景中,重写父类的方法时,可能会考虑到需要重新调用父类的方法,所以super()函数就是比较使用也很必要的解决方法: 文章来源: http://www.cnblogs.com/ ...
- python super()函数:调用父类的构造方法
python子类会继承父类所有的类属性和类方法.严格来说,类的构造方法其实就是实例方法,因此,父类的构造方法,子类同样会继承. 我们知道,python是一门支持多继承的面向对象编程语言,如果子类继承的 ...
- Python setattr() 函数 ,Python super() 函数: Python 内置函数 Python 内置函数
描述 setattr 函数对应函数 getatt(),用于设置属性值,该属性必须存在. 语法 setattr 语法: setattr(object, name, value) 参数 object -- ...
- Python中super函数的用法
之前看python文档的时候发现许多单继承类也用了super()来申明父类,那么这样做有何意义? 从python官网文档对于super的介绍来看,其作用为返回一个代理对象作为代表调用父类或亲类方法.( ...
- python之super()函数
python之super()函数 python的构造器奇特, 使用魔方. 构造器内对基类对象的初始化同样也很奇特, 奇特到没有半点优雅! 在构造器中使用super(class, instance)返回 ...
- 由Python的super()函数想到的
python-super *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !im ...
- Python面试题之Super函数
这是个高大上的函数,在python装13手册里面介绍过多使用可显得自己是高手 23333. 但其实他还是很重要的. 简单说, super函数是调用下一个父类(超类)并返回该父类实例的方法. 这里的下一 ...
- python 面向对象十一 super函数
python 面向对象十一 super函数 super函数用来解决钻石继承. 一.python的继承以及调用父类成员 父类: class Base(object): def __init__(se ...
随机推荐
- Java与C++简单对比
Java语言让编程者无法找到指针来直接访问内存,并且增添了自动的内存管理功能,从而有效的组织了C/C++语言中指针操作失误,如滥用指针所造成的系统崩溃,Java的指针在虚拟机内部使用,这保证了Java ...
- UBUNTU 测试跑分
time echo "scale=5000; 4*a(1)" | bc -l -q3.14159265358979323846264338327950288419716939937 ...
- Spring异步调用注解@Async的使用
1.pom依赖 <dependency> <groupId>org.springframework</groupId> <artifactId>spri ...
- UVALive - 6434 (贪心)
题目链接:https://vjudge.net/problem/UVALive-6434 题意:给你n个数字,要你把这n个数字分成m组,每一组的消耗值定义为改组最大值和最小值之差,要求这m组的消耗值总 ...
- JavaWeb:一个Servelt多个请求
一个Servelt多个请求 基础模拟 方法一:使用switch方法 一.方法介绍 方法:switch 优点:方法简单,明了 缺点:维护麻烦,保密性不好 二.代码实现 1.servlet类 packa ...
- python自学第三天,列表
1.列表 names=[] #这就是一个空列表 names=[1,5,2,3,4,5]#列表是用的中括号,每个元素是用逗号分开的.列表里面的元素是可以重复的. names[-1]#表示的是取列表的最后 ...
- 客户端优化之使用javascript原生方法替代复杂的数学运算和jquery方法
尽管jQuery等js框架相比原生javascript使用起来极为方便但是为什么在一些大型互联网公司还是一致强调前端开发人员的js基础,因为尽管javascript使用起来可能非常不便不仅体现在语法而 ...
- apache ab 压力测试工具
Apache的ab命令模拟多线程并发请求,测试服务器负载压力,也可以测试nginx.lighthttp.IIS等其它Web服务器的压力.Apache附带的ab工具(使用的PHP环境是WAMP集成环境, ...
- HIVE点滴:group by和distinct语句的执行顺序
同一条语句之中,如果同时有group by和distinct语句,是先group by后distinct,还是先distinct后group by呢? 先说结论:先group by后distinct. ...
- Centos7修改profile文件后导致vi command not find
Centos7修改profile文件后导致vi command not find,原因是profile文件没有配置正确,系统就无法找到精确命令了.解决方法: 1.在命令行中输入:export PATH ...