-
Return a proxy object that delegates method calls to a parent or siblingclass of type. This is useful for accessing inherited methods that havebeen 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 methodresolution search order used by bothgetattr()
andsuper()
. Theattribute is dynamic and can change whenever the inheritance hierarchy isupdated.If the second argument is omitted, the super object returned is unbound. Ifthe second argument is an object,
isinstance(obj, type)
must be true. Ifthe second argument is a type,issubclass(type2, type)
must be true (thisis useful for classmethods).Note
super()
only works for new-style classes.There are two typical use cases for super. In a class hierarchy withsingle inheritance, super can be used to refer to parent classes withoutnaming them explicitly, thus making the code more maintainable. This useclosely parallels the use of super in other programming languages.
The second use case is to support cooperative multiple inheritance in adynamic execution environment. This use case is unique to Python and isnot found in statically compiled languages or languages that only supportsingle inheritance. This makes it possible to implement “diamond diagrams”where multiple base classes implement the same method. Good design dictatesthat this method have the same calling signature in every case (because theorder of calls is determined at runtime, because that order adaptsto changes in the class hierarchy, and because that order can includesibling 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 forexplicit dotted attribute lookups such assuper().__getitem__(name)
.It does so by implementing its own__getattribute__()
method for searchingclasses in a predictable order that supports cooperative multiple inheritance.Accordingly,super()
is undefined for implicit lookups using statements oroperators such assuper()[name]
.Also note that
super()
is not limited to use inside methods. The twoargument form specifies the arguments exactly and makes the appropriatereferences.For practical suggestions on how to design cooperative classes using
super()
, see guide to using super().New in version 2.2.
super
(
type
[,
object-or-type
]
)
3.3. New-style and classic classes
Classes and instances come in two flavors: old-style (or classic) and new-style.
Up to Python 2.1 the concept of class
was unrelated to the concept oftype
, and old-style classes were the only flavor available. For anold-style class, the statement x.__class__
provides the class of x, buttype(x)
is always <type 'instance'>
. This reflects the fact that allold-style instances, independent of their class, are implemented with a singlebuilt-in type, called instance
.
New-style classes were introduced in Python 2.2 to unify the concepts ofclass
and type
. A new-style class is simply a user-defined type,no more, no less. If x is an instance of a new-style class, then type(x)
is typically the same as x.__class__
(although this is not guaranteed – anew-style class instance is permitted to override the value returned forx.__class__
).
The major motivation for introducing new-style classes is to provide a unifiedobject model with a full meta-model. It also has a number of practicalbenefits, like the ability to subclass most built-in types, or the introductionof “descriptors”, which enable computed properties.
For compatibility reasons, classes are still old-style by default. New-styleclasses are created by specifying another new-style class (i.e. a type) as aparent class, or the “top-level type” object
if no other parent isneeded. The behaviour of new-style classes differs from that of old-styleclasses in a number of important details in addition to what type()
returns. Some of these changes are fundamental to the new object model, likethe way special methods are invoked. Others are “fixes” that could not beimplemented before for compatibility concerns, like the method resolution orderin case of multiple inheritance.
While this manual aims to provide comprehensive coverage of Python’s classmechanics, it may still be lacking in some areas when it comes to its coverageof new-style classes. Please see https://www.python.org/doc/newstyle/ forsources of additional information.
Old-style classes are removed in Python 3, leaving only new-style classes.