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.
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用法及原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 概念 super作为python的内建函数。主要作用如下: 允许我们...
Python中的`super()`函数是一个非常重要的特性,尤其在面向对象编程中用于处理继承关系。`super()`函数的主要作用是调用父类(超类)的方法,使得子类可以在不直接引用父类的情况下扩展或覆盖父类的功能。在Python中...
使用方法: 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()`函数的主要目的是简化代码,避免直接引用父类名称,这样可以使代码更加灵活,减少硬编码,并有助于实现多...
在Python编程语言中,`super()`方法是一个非常重要的特性,特别是在处理类的继承关系时。本文将详细解析`super()`方法的工作原理,并通过实例来解释其用途。 首先,`__init__()`函数是Python中的构造方法,用于初始...
### Python `super()` 函数与多重继承详解 #### 一、`super()` 函数简介 在面向对象编程中,继承是一种常见的设计模式,它允许一个类(子类)继承另一个类(父类)的属性和方法。在 Python 中,`super()` 函数是一...
在深入理解Python的过程中,我们需要了解多个方面的知识,包括但不限于Python的环境搭建、基础语法、数据类型、函数、模块、面向对象编程、文件操作、网络编程、以及Web开发等。以下是根据文档内容总结的详细知识点...
“8.21Python super()使用注意事项.html”讨论了super()函数的使用,它用于调用父类的方法,尤其是在多继承场景下,能确保正确的调用顺序。 “9.14Python生成器(send,close,throw)方法详解.html”涉及Python的...
### 解决Python `super()` 调用多重继承函数的问题 在Python中,`super()` 函数被广泛用于实现多重继承。它可以帮助我们避免代码重复,并使得类的继承结构更加清晰。然而,在处理复杂的多重继承关系时,如何正确地...
**Python-SuperPoint兴趣点检测与描述自监督训练框架** SuperPoint是一款先进的计算机视觉算法,主要应用于图像处理和特征匹配领域。它通过自监督学习的方式训练出能够进行精确兴趣点检测和描述的神经网络模型。在...
Python的`super()`函数是一个非常重要的工具,尤其在处理类的继承关系时。它主要用于调用父类(超类)的方法,确保代码的简洁性和可维护性。在Python 3中,所有类都是新式类,因此我们可以充分利用`super()`的优势。...
Python中的`super`关键字是用来解决继承关系中调用父类方法的一个重要工具,尤其是在处理多继承的情况下。在Python 2.2之前,调用父类方法通常需要直接使用父类的名称并传入`self`参数,这在类结构发生变化时容易...
本主题主要关注使用Python实现的Super-Resolution方法,特别是"Super Pix"这种特定的技术。下面我们将深入探讨相关的知识。 1. **图像清晰化**:图像清晰化是图像处理中的一个重要环节,其目标是提高图像的可读性和...
Python中的`super()`方法是用来处理多重继承问题的关键工具,它使得子类能够便捷地调用父类的方法,尤其是在复杂的继承结构中。理解`super()`的工作原理对于编写清晰、高效的多继承代码至关重要。 首先,让我们澄清...