概念:

  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. python psycopg2 连接pg 建立连接池

    # -*- coding: utf-8 -*-from psycopg2.pool import ThreadedConnectionPool,SimpleConnectionPool,Persist ...

  2. prefProvider.kt

    package com.gh0u1l5.wechatmagician.frontend import android.content.ContentProvider import android.co ...

  3. GUI学习之八——复选框QCheckBox的学习总结

    一.描述 a.QCheckBox一般用于给用户提供若干选项中多个选择时的使用 b.控件左侧有一个方框来显示控件被选中. c.复选框是有三种状态的 二.使用 1.创建 复选框的创建和常规的按钮创建方式是 ...

  4. WCF系列_WCF如何选择不同的绑定

    内容转载自<WCF核心技术> 开发者不用直接操作信道范型,而是由WCF根据服务OperationContract来选择合适的信道范型.大多数信道范型都有无会话两种变体.有会话信道会在客户端 ...

  5. 关于oracle 索引,收藏

    该篇文章很好,,收藏了.. https://www.cnblogs.com/liangyihui/p/5886619.html oracle 索引建立: create  bitmap/UNIQUE i ...

  6. LNMP支持 多版本PHP

    1.到 http://www.php.net/downloads.php(http://www.php.net/downloads.php) 选择合适的版本号,如 5.6.34 2.到 LNMP 1. ...

  7. EL和JSTL笔记

    1.EL表达式 [1] 简介 > JSP表达式 <%= %> 用于向页面中输出一个对象. > 到JSP2.0时,在我们的页面中不允许出现 JSP表达式和 脚本片段. > ...

  8. 【ElasticSearch】 elasticsearch-head插件安装

    本章介绍elasticsearch-head插件安装,elasticsearch安装参考:[ElasticSearch] 安装 elasticsearch-head安装和学习可参照官方文档: http ...

  9. Vue 去掉#号,让网址像正常的一样

    vue利用hash模式让页面不刷新,但是有时候看起来觉得怪怪的,也可以去掉#,并像#模式一样不刷新页面. 1.在路由里面添加     mode: 'history' 这样就去掉了#号,但是点击页面会发 ...

  10. Linux---基础指令(一)

    https://www.linuxprobe.com/chapter-02.html  (Linux就要这么学) 一.执行查看帮助命令 date:date命令用于显示及设置系统的时间或日期,格式为“d ...