Python super() 函数

Python super() 函数

 Python 内置函数


描述

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: def add(self, x): y = x+1 print(y) class B(A): def add(self, x): super().add(x) b = B() b.add(2) # 3

Python2.x 实例:

实例

#!/usr/bin/python # -*- coding: UTF-8 -*- class A(object): # Python2.x 记得继承 object def add(self, x): y = x+1 print(y) class B(A): def add(self, x): super(B, self).add(x) b = B() b.add(2) # 3

返回值

无。


实例

以下展示了使用 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.

原文地址:https://www.cnblogs.com/fpzs/p/10476875.html

时间: 2024-07-29 23:48:12

Python super() 函数的相关文章

由Python的super()函数想到的

python-super *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* BLOCKS =============================================================================*/ p, blockquote, ul, ol, dl, table, pre { margin: 15px

关于Python中的类普通继承与super函数继承

关于Python中的类普通继承与super函数继承 1.super只能用于新式类 2.多重继承super可以保公共父类仅被执行一次 一.首先看下普通继承的写法 二.再看看super继承的写法 参考链接:http://blog.csdn.net/lqhbupt/article/details/19631991

python 面向对象十一 super函数

super函数用来解决钻石继承. 一.python的继承以及调用父类成员 父类: class Base(object): def __init__(self): print("base init.") 普通方法调用父类: class Leaf(Base): def __init__(self): Base.__init__(self) print("Leaf init.") super方法调用父类: class Leaf(Base): def __init__(se

python面向对象super函数

python面向对象super函数 待办 python面向对象可以多继承,同时集成的两个类继承自同一个父类的时候初始化问题,提前定义初始顺序,如果有相同的就按顺序初始化,不要把super理解成父类 https://blog.csdn.net/robinjwong/article/details/48369615 原文地址:https://www.cnblogs.com/lishikai/p/12382320.html

python基础----多态与多态性、super函数用法、继承原理

目录: 一.多态与多态性 二.super函数用法 三.继承原理 一.多态与多态性                                                                        ㈠多态: 多态指的是一类事物有多种形态,(一个抽象类有多个子类,因而多态的概念依赖于继承) 1. 序列类型有多种形态:字符串,列表,元组. 2. 动物有多种形态:人,狗,猪 1 import abc 2 class Animal(metaclass=abc.ABCMeta):

python之类中的super函数

作用 实现代码重用 思考:super真的只是调用父类么? super函数是按照mro算法去调用的,不bb上代码: class A: def __init__(self): print('A') class B(A): def __init__(self): print('B') super().__init__() class C(A): def __init__(self): print('C') super().__init__() class D(B, C): def __init__(s

python的函数

一 函数是什么? 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,具体区别,我们后面会讲,编程中的函数在英文中也有很多不同的叫法.在BASIC中叫做subroutine(子过程或子程序),在Pascal中叫做procedure(过程)和function,在C中只有function,在Java里面叫做method. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也可以自己创建函数,这被叫做用户自定义函数. 定

Python super初始化理解过程

Python中子类调用父类的方法有两种方法能够实现:调用父类构造方法,或者使用super函数(两者不要混用). 使用“super”时经常会出现代码“super(FooChild,self).__init__(xxx,xxx)”.那super().__init__()到底要怎么用呢? 为了方便理解,我写了如下例子验证用法: #-*- coding:utf-8 -*-class A(object): def __init__(self,xing,gender): #!#1 self.namea="a

python之函数用法capitalize()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法capitalize() #capitalize() #说明:将字符串的第一个字母变成大写,其他字母变小写. ''' capitalize(...) S.capitalize() -> string Return a copy of the string S with only its first character capitalized. ''' #案例 str='xiaoden