super

super(type, object_or_type=None)

Returns a proxy object that delegates method calls to a parent or sibling class of a specified type.

Parameters

  • type: Type from which to start the search for the proxy object. Previously, this was mandatory.
  • obj-or-type: If not specified, an unbound proxy object is returned. If an object is provided, a proxy for obtaining the object’s method is returned, where isinstance(obj, type) is True. If a type is provided, a proxy for obtaining the class method is returned, where issubclass(subtype, type) is True. It is used to access base implementations of inherited methods overridden in the subclass. The method search order is the same as with getattr(), except the type is skipped. The type has an mro attribute that specifies the method search order used by both getattr() and super(). This attribute is dynamic and can change as the inheritance hierarchy is updated.

Typical Use Cases for super()

  • In single inheritance hierarchies, it refers to parent classes without explicitly naming them, simplifying code maintenance. This usage has counterparts in other programming languages.
  • It supports cooperative multiple inheritance in a dynamic environment. This variant is unique to Python and absent in statically compiled languages or those supporting only single inheritance. It enables handling diamond-shaped hierarchies (see diamond problem), where multiple base classes provide implementations of a method with the same name. Good design principles require that the method has the same signature in all cases, as the order of calls is determined at runtime. This order is set when the hierarchy changes, and siblings unknown until runtime may be present in the chain.

Return Value

Returns a proxy object that delegates method calls to a parent or sibling class of a specified type.

Examples

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

Without arguments, super() works only within the class definition: the compiler fills in the details necessary for identifying the class and accessing its object’s methods.