🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

super() Function

AI & DATA SCIENCE // super-function

super() returns a proxy object that lets a subclass call methods from its parent class, most commonly to extend __init__ rather than fully overriding it.

Syntax

class Child(Parent):
    def __init__(self, x, y):
        super().__init__(x)
        self.y = y

Deep Dive Course

Calling a method through super() inside a subclass looks up and calls that method on the parent class, or, more precisely, the next class in the method resolution order for multiple inheritance, automatically passing the current instance along. It's most commonly used inside a subclass's __init__ to run the parent's setup logic before adding the subclass's own attributes, which avoids duplicating the parent's initialization code. Without arguments, super() automatically figures out the current class and instance from context — the explicit two-argument form is the older, more verbose Python 2-compatible syntax.

1Understanding super() Function

Calling a method through super() inside a subclass looks up and calls that method on the parent class, or, more precisely, the next class in the method resolution order for multiple inheritance, automatically passing the current instance along. It's most commonly used inside a subclass's __init__ to run the parent's setup logic before adding the subclass's own attributes, which avoids duplicating the parent's initialization code. Without arguments, super() automatically figures out the current class and instance from context — the explicit two-argument form is the older, more verbose Python 2-compatible syntax.

💡

Forgetting to call the parent's __init__ through super() in a subclass's constructor is a common bug: the subclass gets its own new attributes but silently skips whatever setup the parent class expected to happen, leaving the object partially initialized.

editor.html
class Vehicle:
    def __init__(self, wheels):
        self.wheels = wheels

class Car(Vehicle):
    def __init__(self, brand):
        super().__init__(wheels=4)
        self.brand = brand

c = Car("Toyota")
print(c.wheels, c.brand)
localhost:3000

2Practical Example

Here is a real-world application of super() Function showing how it is used in production Python code.

editor.html
class Logger:
    def log(self, message):
        print(f"[LOG] {message}")

class TimestampedLogger(Logger):
    def log(self, message):
        super().log(f"2026-01-01 {message}")

TimestampedLogger().log("Server started")
localhost:3000

3Best Practices

Follow these guidelines when working with super() Function:

1. Call super().__init__(...) as the first line of a subclass's constructor, before setting up the subclass's own attributes

2. Use the zero-argument super() form in Python 3 instead of the older two-argument syntax

3. Use super() to extend a parent method's behavior (call it, then add more) rather than duplicating the parent's logic inside the override

⚠️

Tip: Forgetting to call the parent's __init__ through super() in a subclass's constructor is a common bug: the subclass gets its own new attributes but silently skips whatever setup the parent class expected to happen, leaving the object partially initialized.

editor.html
class Vehicle:
    def __init__(self, wheels):
        self.wheels = wheels

class Car(Vehicle):
    def __init__(self, brand):
        super().__init__(wheels=4)
        self.brand = brand

c = Car("Toyota")
print(c.wheels, c.brand)
localhost:3000

Examples

Example 01Basic Usage
class Vehicle:
    def __init__(self, wheels):
        self.wheels = wheels

class Car(Vehicle):
    def __init__(self, brand):
        super().__init__(wheels=4)
        self.brand = brand

c = Car("Toyota")
print(c.wheels, c.brand)
Example 02Advanced Example
class Logger:
    def log(self, message):
        print(f"[LOG] {message}")

class TimestampedLogger(Logger):
    def log(self, message):
        super().log(f"2026-01-01 {message}")

TimestampedLogger().log("Server started")

Best Practices

  • Call super().__init__(...) as the first line of a subclass's constructor, before setting up the subclass's own attributes
  • Use the zero-argument super() form in Python 3 instead of the older two-argument syntax
  • Use super() to extend a parent method's behavior (call it, then add more) rather than duplicating the parent's logic inside the override

Interview Question

Why is calling the parent's __init__ through super() usually the first line of a subclass's constructor, rather than the last?

Hint: Think about what state the parent's setup logic expects to exist.

The parent's __init__ typically establishes attributes and invariants that the rest of the object's setup, including the subclass's own __init__ code, may depend on. Calling it first ensures the object is in a valid, parent-initialized state before the subclass adds anything on top of it, avoiding bugs where subclass setup code accidentally runs before attributes it needs actually exist.

Exercises

MediumPractice using super() Function in a real scenario.
View Solution
class Vehicle:
    def __init__(self, wheels):
        self.wheels = wheels

class Car(Vehicle):
    def __init__(self, brand):
        super().__init__(wheels=4)
        self.brand = brand

c = Car("Toyota")
print(c.wheels, c.brand)

Frequently Asked Questions

Why is calling the parent's __init__ through super() usually the first line of a subclass's constructor, rather than the last?

The parent's __init__ typically establishes attributes and invariants that the rest of the object's setup, including the subclass's own __init__ code, may depend on. Calling it first ensures the object is in a valid, parent-initialized state before the subclass adds anything on top of it, avoiding bugs where subclass setup code accidentally runs before attributes it needs actually exist.

Related Functions

inheritanceinitpolymorphism