super()

The super() function returns a temporary object that allows a given class to inherit the methods and properties of a parent or sibling class.

Syntax

super(type, obj)

The type parameter specifies the type object of the parent class. The obj is optional and is an instance, or subtype, of the class type.

Inside Class Definition

When used inside a class definition, the super() function can be used with zero arguments since the given class inherits from a parent class:

class A():
  method(self, arg):
    #Method code starts here

class B(A):
  method(self, arg):
    super().method(arg)
    #Method code starts here

The super() function allows the child class B to access the .method() of parent class A.

Outside Class Definition

The following syntax can be applied inside and outside of a class definition:

class B(A):
  method(self, arg):
    super().method(arg)
    #Method code starts here

instance_of_b = B()
super(B, instance_of_b).method()

The super() function accepts the class type B along with an instance_of_b variable to direct the lookup search for a .method() in the nearest parent class.

Codebyte Example

In the following example, both syntaxes of super() are implemented in the classes Python and Java to access the intro() method of the parent ProgramLanguage class:

Code
Output
Loading...

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Python on Codecademy

Contributors