super() 函数是用于调用父类(超类)的一个方法。
super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO)、重复调用(钻石继承)等种种问题。
MRO 就是类的方法解析顺序表, 其实也就是继承父类方法时的顺序表。
语法
以下是 super() 方法的语法:
super(type[, object-or-type])
参数
type -- 类。
object-or-type -- 类,一般是 self
Python3.x 和 Python2.x 的一个区别是: Python 3 可以使用直接使用 super().xxx 代替 super(Class, self).xxx :
Python3.x 实例:
class A:
pass
class B(A):
def add(self, x):
super().add(x)
Python2.x 实例:
class A(object): # Python2.x 记得继承 object
pass
class B(A):
def add(self, x):
super(B, self).add(x)
返回值
无。 实例
以下展示了使用 super 函数的实例:
#!/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() 用法

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

  2. python super

    http://hi.baidu.com/thinkinginlamp/item/3095e2f52c642516ce9f32d5 Python中对象方法的定义很怪异,第一个参数一般都命名为self(相 ...

  3. python内置方法

    1. 简介 本指南归纳于我的几个月的博客,主题是 魔法方法 . 什么是魔法方法呢?它们在面向对象的Python的处处皆是.它们是一些可以让你对类添加"魔法"的特殊方法. 它们经常是 ...

  4. 【转】Python的神奇方法指南

    [转]Python的神奇方法指南 有关Python内编写类的各种技巧和方法(构建和初始化.重载操作符.类描述.属性访问控制.自定义序列.反射机制.可调用对象.上下文管理.构建描述符对象.Picklin ...

  5. super超类继承特点小结

    super超类继承特点小结: 1. super并不是一个函数,是一个类名,形如super(B, self)事实上调用了super类的初始化函数,产生了一个super对象: 2. super类的初始化函 ...

  6. python之魔法方法介绍

    1.1. 简介 什么是魔法方法呢?它们在面向对象的Python的处处皆是.它们是一些可以让你对类添加“魔法”的特殊方法. 它们经常是两个下划线包围来命名的(比如 __init__ , __lt__ ) ...

  7. python super()函数详解

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

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

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

  9. python——super()

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

随机推荐

  1. python-作用域解析

    局部作用域和全局作用域:局部作用域不能修改全局作用域的变量 count = 10 def outer(): #global count 局部变量改成全局变量,global声明一下即可.就可以修改了. ...

  2. linux debug tools

    linux modules: IO schedule.VFS.OOM.memory.net.process schedule kernel debug: perf(IO bound\CPU bound ...

  3. 关于list中移除某种数据类型的方法

    众所周知,list在不泛型的情况下是可以存放各种数据类型的,代码如下: public static void main(String[] args) { List list=new ArrayList ...

  4. Linux基础命令mkdir

    mkdir  /home/ceshi   home目录下创建测试目录 mkdir  -p  /home/ceshi1/ceshi home目录下递归创建 mkdir     /home/ceshi2  ...

  5. PHP json_decode为什么将json字符串转成数组是对象格式?

    eg. $a='[{\"img\":\"/uploads/agency/carimgs/5/15515954778091.jpg\"},{\"img\ ...

  6. angular 引入ocLazyLoad实现js、controller懒加载

    项目之前是直接在index.html中引用了controller.js,其中包含了所有的controller.现引入ocLazyLoad实现按需加载,到指定页面再加载指定js.controller 1 ...

  7. ArcGIS中KML转为shp文件

    问题:如何将KML转为shp文件? 方法: 1.打开ArcMap -> ArcToolbox: 2.在ArcToolbox中选择“转换工具”-> “由KML转出” -> “KML转图 ...

  8. Vagrant 入门指南

    https://blog.csdn.net/qianghaohao/article/details/80038096 https://blog.csdn.net/happyhorizion/artic ...

  9. 启动Cognos时报0106错误

    1. 问题描述 启动Cognos失败,报错代码为0106. 2. 问题分析 是jdk版本不兼容. 3. 解决方案 方案一:更换jdk1.6,可以使用免安装版,不需要卸载原有的jdk将java_home ...

  10. idea crack

    ThisCrackLicenseId-{ “licenseId”:”11011”, “licenseeName”:”Wechat”, “assigneeName”:”tree-deep-see-dee ...