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 ...
随机推荐
- powerdesigner 使用
1. 首先要确保机器安装了MySql的ODBC驱动,去http://dev.mysql.com/downloads/connector/odbc/5.1.html 下载 Connector/ODBC ...
- 牛客第三场多校 E Sort String
链接:https://www.nowcoder.com/acm/contest/141/E来源:牛客网 Eddy likes to play with string which is a sequen ...
- <Spark><Spark Streaming><作业分析><JobHistory>
Intro 这篇是对一个Spark (Streaming)作业的log进行分析.用来加深对Spark application运行过程,优化空间的各种理解. Here to Start 从我这个初学者写 ...
- day 68 增删改查 语法
1 普通正则 2 分组正则 url(r'/blog/(\d+)/(\d+)',views.blog) blog(request,arq1,arq2) 按照位置传参 3 分组命名 url(r'/ ...
- filebeat成精之路
https://www.cnblogs.com/jingmoxukong/p/8185321.html
- mod_fcgid: HTTP request length 136136 (so far) exceeds MaxRequestLen (131072)
原来是fastcgi模式下的设置问题,需要在配置文件.htaccess或者直接在apache的配置文件http.conf 中指明,如下: 查看官方说明有这么一句:Default: FcgidMaxRe ...
- HTTP、TCP、IP、协议
HTTP(HyperText Transfer Protocol) 即超文本传输协议,现在基本上所有web项目都遵从HTTP协议(协议就是一种人为的规范). 目前绝大部分使用的都是HTTP/1.1版本 ...
- Oralce 11g新特性 转载
Oracle 11g于2007年7月11日美国东部时间11时(北京时间11日22时)正式发布,11g是甲骨文公司30年来发布的最重要的数据库版本,根据用户的需求实现了信息生命周期管理(Informat ...
- CRM工具简介
pacemaker是高可用集群中的CRM(Cluster Resource Manager)资源管理层,他是一个服务,可以作为一个单独的服务启动,不过在如果使用corosync1.4中,我们可以设置c ...
- NOI-1.3-11-计算浮点数相除的余数
11:计算浮点数相除的余数 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 计算两个双精度浮点数a和b的相除的余数,a和b都是正数的.这里余数(r)的定义是: ...