概念:

  super() 函数是用于调用父类(超类)的一个方法。

super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO)、重复调用(钻石继承)等种种问题。

 格式:

super(type[, object-or-type])
  • type -- 类。
  • object-or-type -- 类,一般是 self
  • Python3.x 和 Python2.x 的一个区别是: Python 3 可以使用直接使用 super().xxx 代替 super(Class, self).xxx :

  简单实例:

  可以说明supper的概念和使用方法.

class Base(object):
def __init__(self):
print "Base created" class ChildA(Base):
def __init__(self):
Base.__init__(self) class ChildB(Base):
def __init__(self):
super(ChildB, self).__init__() ChildA()
ChildB()

  输出结果:

Base created
Base created

  高级实例:

#!/usr/bin/python
# -*- coding: UTF-8 -*- class FooParent(object):
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) 首先找到 FooChild 的父类(就是类 FooParent),然后把类B的对象 FooChild 转换为类 FooParent 的对象
super(FooChild,self).__init__()
print ('Child') def bar(self,message):
super(FooChild, self).bar(message)
print ('Child bar fuction')
print (self.parent) if __name__ == '__main__':
fooChild = FooChild()
fooChild.bar('HelloWorld')

  输出结果:

Parent
Child
HelloWorld from Parent
Child bar fuction
I'm the parent.

Python super() 函数的概念和例子的更多相关文章

  1. Python super() 函数

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

  2. python super()函数详解

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

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

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

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

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

  5. Python面试题之Super函数

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

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

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

  7. python学习之路---day20--面向对象--多继承和super() 函数

    一:python多继承 python多继承中,当一个类继承了多个父类时候,这个类拥有多个父类的所欲非私有的属性 l例子: class A: pass class B(A): pass class C( ...

  8. python函数纯概念汇总(一)

    在使用python的时候由于前期基本概念没有分清楚,所以需要重新归纳汇总学一学. 一.什么是函数: 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,编程中的函数在英文中也有很 ...

  9. 认识python中的super函数

    需求分析 在类继承中,存在这么一种情况: class Human(object): def Move(self): print("我会走路...") class Man(Human ...

随机推荐

  1. 3D Graph Neural Networks for RGBD Semantic Segmentation

    3D Graph Neural Networks for RGBD Semantic Segmentation 原文章:https://www.yuque.com/lart/papers/wmu47a ...

  2. js最实用string(字符串)类型的使用及截取与拼接详解

    var a = '世界上最远的距离不是天涯海角'; 一.通过字符获取位置或通过位置获取字符: //指定位置返回字符console.log(str.charAt(1));console.log(str[ ...

  3. HTML与盒模型

    EC前端 - HTML教程 HTML与盒模型 HTML结构 <!doctype html> <html> <head> <meta charset=" ...

  4. MySQL事物管理

    sql语句对数据库操作构成事物. 事物的特性:ACID •Atomicity(原子性):一个事物要么全都被执行,要么全都不执行,不会存在执行结束在中间环节.如果事物在执行过程中发生异常,则会回滚到事物 ...

  5. (PMP)解题技巧和典型题目分析(每日20题)

    3.11 1.A(C),2.D,3.A,4.B,5.A(C),6.D(A),7.D,8.A(D),9.B,10.D(B), 11.C(B),12.C(D),13.B,14.D,15.C,16.C(D) ...

  6. Android事件处理的三种方法

    一.基于监听 setOnClickListener,setOnLongClickListener.setOnTouchListener 注意:如果onTouchEvent方法return true,则 ...

  7. XSS攻击 CSRF攻击

    XSS攻击: 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆, 故将跨站脚本攻击缩写为XSS.恶意攻击者 ...

  8. eclipse设置新建jsp文件默认字符编码为utf-8

    在使用Eclipse开发中,编码默认是ISO-8859-1,不支持中文.这样我们每次新建文件都要手动修改编码,非常麻烦.其实我们可以设置文件默认编码,今后再新建文件时就不用修改编码了. 1.打开Ecl ...

  9. poj 1026

    这题一开始没看清楚 等级差距不超过1 1->2->3 就是错误的,因为3-1==2 ,意思是间接的也不行 其次等级最小是1,最大是n 你要到达1号首领的位置 假设1号等级x,限制m,最大上 ...

  10. win32控制台程序 宽字符与短字符转化

    由于vs各版本之间存在字符设置不兼容问题,特总结char与tchar的互相转换函数,如下,在之后的工程中可以使用. void TcharToChar(const TCHAR * tchar, char ...