美文网首页胶水Python
Python:super函数

Python:super函数

作者: 爱折腾的大懒猪 | 来源:发表于2019-04-29 22:26 被阅读2次

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

help介绍如下:

super(type, obj) -> bound super object; requires isinstance(obj, type)
super(type) -> unbound super object
super(type, type2) -> bound super object; requires issubclass(type2, type)
Typical use to call a cooperative superclass method:
class C(B):
    def meth(self, arg):
        super(C, self).meth(arg)

由此可知, super有三种用法, 第一参数总是召唤父类的那个类, 第二参数可缺(返回非绑定父类对象),也可以是实例对象或该类的子类. 最终返回的都是父类的实例(绑定或非绑定). 在Python3中,super函数多了一种用法是直接super(),相当于super(type,首参), 这个首参就是一般的传入的self实例本身啦. 因为在py2里面常用也是这种写法.

另外, 在py2中, super只支持新类( new-style class, 就是继承自object的).

为什么要调用父类?

相关文章

  • Python super() 函数

    描述 super() 函数是用于调用父类(超类)的一个方法。super 是用来解决多重继承问题的,直接用类名调用父...

  • Python super() 函数

  • Python super() 函数

    super() 函数用于调用下一个父类(超类)并返回该父类实例的方法。 super 是用来解决多重继承问题的,直接...

  • Python super() 函数

    描述 super() 函数是用于调用父类(超类)的一个方法。 super 是用来解决多重继承问题的,直接用类名调用...

  • Python:super函数

    这是个高大上的函数,在python装13手册里面介绍过多使用可显得自己是高手 23333. 但其实他还是很重要的....

  • python super()函数

    super()函数是用于调用父类(超类)的一个方法。super()是用来解决多重继承问题的,直接用类名调用父类方法...

  • 怎么理解Python类中的super函数

    前言 在Python类的继承中,经常能看到super函数的存在,那super函数主要的作用,以及如何理解和使用好这...

  • Python super函数详解

    Python中对象方法的定义很怪异,第一个参数一般都命名为self(相当于其它语言的this),用于传递对象本身,...

  • python super()和classmethod

    @(python) super(type, obj) 子类中定义了同父类同名的函数后,需要显示调用父类函数时,可以...

  • python之super()函数的用法

    title: python之super()函数的用法date: 2018-12-22 21:56:23catego...

网友评论

    本文标题:Python:super函数

    本文链接:https://www.haomeiwen.com/subject/jlkpnqtx.html