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:
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
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:
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:
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:
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.
Related Resources
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().
For additional information on related topics, take a look at the following resources:
- Inheritance and Composition: A Python OOP Guide (Tutorial)
- Object-Oriented Programming (OOP) in Python (Tutorial)
- Python Classes: The Power of Object-Oriented Programming (Tutorial)
- Supercharge Your Classes With Python super() (Course)
- Supercharge Your Classes With Python super() (Quiz)
- Inheritance and Composition: A Python OOP Guide (Course)
- Inheritance and Composition: A Python OOP Guide (Quiz)
- Intro to Object-Oriented Programming (OOP) in Python (Course)
- Object-Oriented Programming (OOP) in Python (Quiz)
- Class Concepts: Object-Oriented Programming in Python (Course)
- Inheritance and Internals: Object-Oriented Programming in Python (Course)
- Python Classes - The Power of Object-Oriented Programming (Quiz)