Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

super()

Obtains a proxy that continues attribute lookup after the current class in the method resolution order.

Version: Python 3.14; repository image, production runtime not attested

Syntax

super()
super(type, object_or_type=None, /)

Obtains a proxy that continues attribute lookup after the current class in the method resolution order.

In cooperative multiple inheritance the next implementation can belong to a sibling class, depending on the instance’s full hierarchy.

Zero-argument super needs a lexical __class__ cell and the current function’s first argument; a nested argumentless lambda raises RuntimeError. The explicit parameters are positional-only. The proxy starts attribute lookup just after the named class in the receiver’s method resolution order. For Both the order is Both, Left, Right, Base, object. The zero-argument super inside Left therefore finds Right.path, then Base.path, producing left, right, base. The lookup proceeds along this order, not merely to the first declared parent.

class Probe:
    def call(self): return (lambda: super())()
try:
    Probe().call()
except RuntimeError:
    pass
else:
    raise AssertionError
class A: tag = "A"
class B(A): tag = "B"
class C(B): pass
assert super(B, C()).tag == "A" and super(C, C()).tag == "B"

Examples

class Base:
    def path(self): return ["base"]
class Left(Base):
    def path(self): return ["left"] + super().path()
class Right(Base):
    def path(self): return ["right"] + super().path()
class Both(Left, Right):
    pass

print(Both().path())

Result

['left', 'right', 'base']

Zero-argument super needs a lexical `__class__` cell and the current function’s first argument; a nested argumentless lambda raises RuntimeError. The explicit parameters are positional-only. The proxy starts attribute lookup just after the named class in the receiver’s method resolution order. For Both the order is Both, Left, Right, Base, object. The zero-argument super inside Left therefore finds Right.path, then Base.path, producing left, right, base. The lookup proceeds along this order, not merely to the first declared parent.

Sources