Python super() (original) (raw)

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:

Syntax

super()

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

Why Use super()?

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", "olivia@email.com") print(obj.id, obj.name, obj.email)

`

Output

101 Olivia olivia@email.com

**Explanation:

**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)

`

**Explanation:

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: