GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Inheritance
  • Python MRO

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Python super() - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Python super()

Last Updated : 18 Mar, 2026

In Python, super() function is used to call methods from a parent (superclass) inside a child (subclass). It allows to extend or override inherited methods while still reusing the parent's functionality.

Let's understand how super() works with an example where a child class calls a method from its parent class.

Python
class Parent:
    def show(self):
        print("This is Parent class method")

class Child(Parent):
    def show(self):
        super().show()   # Calling parent method
        print("This is Child class method")

obj = Child()
obj.show()

Output
This is Parent class method
This is Child class method

Explanation:

  • Parent class has a method show() and child class overrides the same method.
  • super().show() calls the parent class method inside the child class.
  • This allows the child class to reuse the parent’s functionality and then add its own behavior.

Syntax

super()

Return: Return a proxy object which represents the parent's class.

Why Use super()?

  • No need to hardcode parent class names, useful when class hierarchies change.
  • Works with single, multiple, and multilevel inheritance.
  • Improves code reusability and maintainability.
  • Prevents duplicate initialization in complex hierarchies.

Initialize Parent Attributes with super()

In this example, child class uses super() to call the parent class constructor, allowing it to initialize inherited attributes while also adding its own new attribute.

Python
class Emp:
    def __init__(self, id, name):
        self.id = id
        self.name = name

class fun(Emp):
    def __init__(self, id, name, email):
        super().__init__(id, name)   #Calls Emp’s __init__
        self.email = email

obj = fun(101, "Olivia", "[email protected]")
print(obj.id, obj.name, obj.email)

Output
101 Olivia [email protected]

Explanation:

  • Emp is the parent class with attributes id and name.
  • fun inherits from Emp and adds an email attribute.
  • super().__init__(id, name) initializes the parent attributes without rewriting the constructor.

Note: The position of super().__init__() affects the order in which the parent and child class code executes. Placing it before or after other statements changes whether the parent initialization runs first or later.

Effect of Using and Not Using super()

Example 1: In this example, child class overrides the constructor but does not call the parent constructor, which results in missing initialization of inherited attributes.

Python
class Person:
    def __init__(self, name, id):
        self.name = name
        self.id = id

class Emp(Person):
    def __init__(self, name, id):
        self.name_ = name   # Forgot to call Person’s __init__

emp = Emp("Jack", 103)
print(emp.name)  

Output

AttributeError: 'Emp' object has no attribute 'name'

Example 2: In this example, super() is used inside the child class constructor to properly call the parent constructor, ensuring that inherited attributes are initialized correctly.

Python
class Person:  
    def __init__(self, name, id):
        self.name = name
        self.id = id

class Emp(Person):  
    def __init__(self, name, id):
        super().__init__(name, id) 

emp = Emp("James", 103)
print(emp.name, emp.id)

Output
James 103

Explanation:

  • Emp overrides the constructor of Person.
  • super().__init__(name, id) calls the parent constructor to initialize inherited attributes.
  • Creating emp = Emp("James", 103) sets both parent and child attributes correctly.

Using super() in Multilevel Inheritance

In a multilevel inheritance setup, super() ensures that each parent class constructor is executed in the correct sequence. It follows Python’s Method Resolution Order (MRO) to maintain a consistent and predictable flow of initialization.

Python
class Mammal:
    def __init__(self, name):
        print(name, "is a mammal")

class CanFly(Mammal):
    def __init__(self, name):
        print(name, "cannot fly")
        super().__init__(name)

class CanSwim(CanFly):
    def __init__(self, name):
        print(name, "cannot swim")
        super().__init__(name)

class Animal(CanSwim):
    def __init__(self, name):
        super().__init__(name)

dog = Animal("Dog")

Output
Dog cannot swim
Dog cannot fly
Dog is a mammal

Explanation:

  • Each class calls super().__init__() to ensure parent constructors run.
  • Execution follows the hierarchy: Animal → CanSwim → CanFly → Mammal.
  • This ensures each parent constructor runs once in the correct order.

Related Articles:

  • Inheritance
  • Python MRO
  • Method Overriding in Python
Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.