Python tutorials > Object-Oriented Programming (OOP) > Inheritance > What is `super()`?
What is `super()`?
The super()
function in Python is used to access methods and properties from a parent or superclass within a child class. It enables you to inherit and extend the functionality of the parent class without directly calling the parent class's name. This is particularly useful in complex inheritance hierarchies.
Basic Syntax and Usage
This code demonstrates the fundamental use of super()
. The Child
class inherits from the Parent
class. In the Child
's __init__
method, super().__init__(name)
calls the Parent
's __init__
method, initializing the name
attribute. Similarly, super().display()
in the Child
's display
method calls the Parent
's display
method.
class Parent:
def __init__(self, name):
self.name = name
def display(self):
print(f"Parent name: {self.name}")
class Child(Parent):
def __init__(self, name, age):
super().__init__(name) # Call the parent's init method
self.age = age
def display(self):
super().display() # Call the parent's display method
print(f"Child age: {self.age}")
child = Child("Alice", 10)
child.display()
Concepts Behind the Snippet
super()
simplifies code by removing the need to explicitly reference the parent class. It promotes code reusability and maintainability by allowing child classes to inherit and extend parent class functionality. It also ensures proper initialization of parent class attributes when inheritance is involved. Without super()
, you would have to manually call the parent's methods using its class name, which becomes cumbersome and error-prone when dealing with multiple inheritance levels.
Real-Life Use Case Section
Consider a scenario where you're creating a graphical application. The Shape
class could represent a generic shape with a color
attribute and a draw
method. The Rectangle
class inherits from Shape
, adding width
and height
attributes. Using super()
in both the __init__
and draw
methods allows the Rectangle
to initialize the color correctly and to extend the drawing behavior of the parent class, avoiding redundant code.
class Shape:
def __init__(self, color):
self.color = color
def draw(self):
print(f"Drawing a shape with color {self.color}")
class Rectangle(Shape):
def __init__(self, color, width, height):
super().__init__(color)
self.width = width
self.height = height
def draw(self):
super().draw()
print(f"Drawing a rectangle with width {self.width} and height {self.height}")
rect = Rectangle("red", 5, 10)
rect.draw()
Best Practices
super()
when initializing the parent class in a child class's __init__
method.super()
to call parent class methods you want to extend or modify in the child class.super()
unnecessarily; only use it when you need to access parent class functionality.
Interview Tip
When asked about super()
in an interview, emphasize its role in simplifying inheritance, promoting code reusability, and ensuring proper initialization of parent classes. Be prepared to explain how it works, provide examples of its use, and discuss potential issues in multiple inheritance scenarios.
When to use them
Use super()
when you are creating a class that inherits from another class and you want to extend or modify the behavior of the parent class. It is particularly useful when you need to initialize the parent class attributes or call parent class methods within the child class.
Memory Footprint
Using super()
itself doesn't significantly impact memory footprint. The memory usage primarily depends on the attributes and methods defined in the classes involved. However, improper use of inheritance and excessive object creation can lead to increased memory consumption.
Alternatives
Instead of super()
, you could directly call the parent class's methods using the class name (e.g., Parent.__init__(self, name)
). However, this approach is less flexible and maintainable, especially in complex inheritance hierarchies. It also breaks if the class name changes.
Pros
Cons
FAQ
-
What happens if I don't use `super()` in the child class's `__init__` method?
If you don't use
super().__init__()
, the parent class's__init__
method will not be called. This means that any attributes initialized in the parent class will not be initialized in the child class, potentially leading to errors or unexpected behavior. -
How does `super()` work in multiple inheritance?
In multiple inheritance,
super()
follows the method resolution order (MRO) to determine which parent class method to call. The MRO is a predictable order in which Python searches for inherited methods. You can inspect the MRO usingChildClass.__mro__
. -
Can I use `super()` outside of a class method?
No,
super()
is designed to be used within a class method, specifically when inheriting from another class. Using it outside of a class context will result in an error.