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()函数的更多相关文章

  1. Python super() 函数的概念和例子

    概念: super() 函数是用于调用父类(超类)的一个方法. super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO).重 ...

  2. Python super() 函数

    super() 函数是用于调用父类(超类)的一个方法. super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果重定义某个方法,该方法会覆盖父类的同名方法,但有时 ...

  3. python super()函数详解

    引言: 在类的多继承使用场景中,重写父类的方法时,可能会考虑到需要重新调用父类的方法,所以super()函数就是比较使用也很必要的解决方法: 文章来源: http://www.cnblogs.com/ ...

  4. python super()函数:调用父类的构造方法

    python子类会继承父类所有的类属性和类方法.严格来说,类的构造方法其实就是实例方法,因此,父类的构造方法,子类同样会继承. 我们知道,python是一门支持多继承的面向对象编程语言,如果子类继承的 ...

  5. Python setattr() 函数 ,Python super() 函数: Python 内置函数 Python 内置函数

    描述 setattr 函数对应函数 getatt(),用于设置属性值,该属性必须存在. 语法 setattr 语法: setattr(object, name, value) 参数 object -- ...

  6. Python中super函数的用法

    之前看python文档的时候发现许多单继承类也用了super()来申明父类,那么这样做有何意义? 从python官网文档对于super的介绍来看,其作用为返回一个代理对象作为代表调用父类或亲类方法.( ...

  7. python之super()函数

    python之super()函数 python的构造器奇特, 使用魔方. 构造器内对基类对象的初始化同样也很奇特, 奇特到没有半点优雅! 在构造器中使用super(class, instance)返回 ...

  8. 由Python的super()函数想到的

    python-super *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !im ...

  9. Python面试题之Super函数

    这是个高大上的函数,在python装13手册里面介绍过多使用可显得自己是高手 23333. 但其实他还是很重要的. 简单说, super函数是调用下一个父类(超类)并返回该父类实例的方法. 这里的下一 ...

  10. python 面向对象十一 super函数

    python 面向对象十一 super函数   super函数用来解决钻石继承. 一.python的继承以及调用父类成员 父类: class Base(object): def __init__(se ...

随机推荐

  1. 1043 输出PATest

    给定一个长度不超过 10​4​​ 的.仅由英文字母构成的字符串.请将字符重新调整顺序,按 PATestPATest.... 这样的顺序输出,并忽略其它字符.当然,六种字符的个数不一定是一样多的,若某种 ...

  2. SharePoint online Multilingual support - Development(1)

    博客地址:http://blog.csdn.net/FoxDave 上一节讲了SharePoint Online网站多语言的实现原理机制,本节主要从编程的角度来谈一下如何进行相关的设置. 下面列出 ...

  3. 【webdriver自动化】整理API框架(主要是关键字,具体例子在本地)

    1. 获取网页源码 pageSource = self.driver.page_source print pageSource.encode("gbk","ignore& ...

  4. SpringBatch Sample (一)(Hello World)

    通过前面两篇关于Spring Batch文章的介绍,大家应该已经对Spring Batch有个初步的概念了.这篇文章,将通过一个”Hello World!”实例,和大家一起探讨关于Spring Bat ...

  5. thinkphp 参数传递方式(基础)

    我今天下午主要学习了thinkphp5.0的路由部分,我下面总结一下我主要学习到的知识点: 路由定义: 有两种方式: (1).动态注册: eg: Route::rule('hello','index/ ...

  6. 20165228 学习基础和C语言基础调查

    ========== 做中学读后感 我依然认为兴趣与自觉性是推动一个人进步的两大因素,他们之间的区别是"兴趣"带来的影响更多是主动性的学习,而"自觉"则是略显被 ...

  7. 创建文件夹c++

    linux #include <sys/types.h> #include <sys/stat.h> string filepath; mkdir(filepath.c_str ...

  8. WCF 添加服务引用 HTTP 请求已超过为 00:00:00 分配的超时。为此操作分配的时间可能是较长超时

    今天在用公司的笔记本引用WCF的时候,处于一直等待的过程,一直在下载信息,一直等了很长时间,弹出了一个消息 下载“http://ip:8085/xxxxx/xxxxx/mex/$metadata”时出 ...

  9. web.net用户控件

    1.它是以 Control注册 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind=& ...

  10. Unity3d mesh合并,网格合并具体用法教程

    Unity开发Mesh合并网格 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分 ...