`
zerxd
  • 浏览: 71450 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

Python super()

 
阅读更多
super(type[, object-or-type])

Return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class. The search order is same as that used by getattr() except that the type itself is skipped.

The __mro__ attribute of the type lists the method resolution search order used by both getattr() and super(). The attribute is dynamic and can change whenever the inheritance hierarchy is updated.

If the second argument is omitted, the super object returned is unbound. If the second argument is an object, isinstance(obj, type) must be true. If the second argument is a type, issubclass(type2, type) must be true (this is useful for classmethods).

Note

super() only works for new-style classes.

There are two typical use cases for super. In a class hierarchy with single inheritance, super can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable. This use closely parallels the use of super in other programming languages.

The second use case is to support cooperative multiple inheritance in a dynamic execution environment. This use case is unique to Python and is not found in statically compiled languages or languages that only support single inheritance. This makes it possible to implement “diamond diagrams” where multiple base classes implement the same method. Good design dictates that this method have the same calling signature in every case (because the order of calls is determined at runtime, because that order adapts to changes in the class hierarchy, and because that order can include sibling classes that are unknown prior to runtime).

For both use cases, a typical superclass call looks like this:

class C(B):
    def method(self, arg):
        super(C, self).method(arg)

Note that super() is implemented as part of the binding process for explicit dotted attribute lookups such as super().__getitem__(name). It does so by implementing its own __getattribute__() method for searching classes in a predictable order that supports cooperative multiple inheritance. Accordingly, super() is undefined for implicit lookups using statements or operators such as super()[name].

Also note that super() is not limited to use inside methods. The two argument form specifies the arguments exactly and makes the appropriate references.

For practical suggestions on how to design cooperative classes using super(), see guide to using super().

New in version 2.2.

 

 

object.__new__(cls[, ...])

Called to create a new instance of class cls__new__() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of __new__() should be the new object instance (usually an instance of cls).

Typical implementations create a new instance of the class by invoking the superclass’s __new__() method using super(currentclass, cls).__new__(cls[, ...]) with appropriate arguments and then modifying the newly-created instance as necessary before returning it.

If __new__() returns an instance of cls, then the new instance’s __init__() method will be invoked like __init__(self[, ...]), where self is the new instance and the remaining arguments are the same as were passed to __new__().

If __new__() does not return an instance of cls, then the new instance’s __init__() method will not be invoked.

__new__() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation.

 

分享到:
评论

相关推荐

    python super()函数的理解(csdn)————程序.pdf

    python super()函数的理解(csdn)————程序

    python super用法及原理详解

    这篇文章主要介绍了python super用法及原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 概念 super作为python的内建函数。主要作用如下: 允许我们...

    python opencv超分辨率重建 4种模型调用

    使用方法: python super_res.py --model models/EDSR_x4.pb --image examples/zebra.png python opencv超分辨率重建 4种模型调用: EDSR_x4.pb ESPCN_x4.pb FSRCNN_x3.pb LapSRN_x8.pb

    python super函数使用方法详解

    python内置函数super()主要用于类的多继承中,用来查找并调用父类的方法,所以在单重继承中用不用 super 都没关系;但是,使用 super() 是一个好的习惯。一般我们在子类中需要调用父类的方法时才会这么用; 二、...

    Python super()方法原理详解

    主要介绍了Python super()方法原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    PythonSuperMario.zip

    This is a python super Mario game that looks exactly like the real super Mario

    Python super()函数使用及多重继承

    主要介绍了Python super()函数使用及多重继承,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    解决python super()调用多重继承函数的问题

    今天小编就为大家分享一篇解决python super()调用多重继承函数的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

    python super()函数的基本使用

    主要介绍了python super()函数的基本使用,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下

    python super的使用方法及实例详解

    主要介绍了python super的使用方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    linux-深入理解python

    传智播客c++学院网络公开课 python公开课资料 文档包含环境搭建、ide使用、python基础、导入c++模块以及web和django

    Python中的super用法详解

    主要介绍了Python中的super用法详解,本文讲解了关于super问题的发现与提出、走进Python的源码世界分析super的实现、延续的讨论super等内容,需要的朋友可以参考下

    Python中super关键字用法实例分析

    本文实例讲述了Python中super关键字用法。分享给大家供大家参考。具体分析如下: 在Python类的方法(method)中,要调用父类的某个方法,在Python 2.2以前,通常的写法如代码段1: 代码段1: class A: def __init__...

    python里 super类的工作原理详解

    super 的工作原理如下: def super(cls, inst): mro = inst.__class__.mro() ...当你使用 super(cls, inst) 时,Python 会在 inst 的 MRO 列表上搜索 cls 的下一个类。 下面看一个例子: class A: def __ini

    深入理解Python中的super()方法

    super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序...这篇文章主要给大家介绍了关于Python中super()方法的相关资料,需要的朋友可以参考下。

Global site tag (gtag.js) - Google Analytics