super()

The built-in super() function provides a way to access methods from a superclass from within a subclass. This function returns a temporary object of the superclass that allows you to call its methods, which is especially useful in implementing inheritance and extending class functionality:

Python
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

super() Signature

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

Arguments

Argument Description Default Value
type The class to start the method resolution order search from. Required argument
object_or_type The object or class whose method resolution order to search. None

Return Value

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

super() Examples

With no arguments:

Python
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

# Usage
square = Square(4)
print(square.length)  # Output: 4

With a two-argument call in a method:

Python
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

class Square(Rectangle):
    def __init__(self, length):
        super(Square, self).__init__(length, length)

# Usage
square = Square(4)
print(square.length)  # Output: 4

super() Common Use Cases

The most common use cases for the super() function include:

  • Accessing methods in a superclass to avoid code duplication
  • Extending and modifying inherited methods in subclasses
  • Facilitating cooperative multiple inheritance by following the method resolution order (MRO)

super() Real-World Example

In a real-world scenario, you might use super() to create a class hierarchy for geometric shapes, such as rectangles and squares. This allows you to reuse and extend existing methods without rewriting them:

Python
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

    def perimeter(self):
        return 4 * self.length

# Usage
square = Square(4)
print(square.area())     # Output: 16
print(square.perimeter()) # Output: 16

In this example, super() is used to initialize the Square class with the properties of the Rectangle class, allowing for efficient reuse of the .area() method.

Tutorial

Supercharge Your Classes With Python super()

In this step-by-step tutorial, you will learn how to leverage single and multiple inheritance in your object-oriented application to supercharge your classes with Python super().

intermediate best-practices python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated Nov. 12, 2024 • Reviewed by Dan Bader