Python super() Function
super()It is a built-in function in Python used to call methods of the parent class (superclass).
In class inheritance,super()it allows you to call parent class methods, enabling code reuse and method extension. This is a very important function in Python object-oriented programming.
Word Definition: superIt means "super", referring here to the parent class or superclass.
Basic Syntax and Parameters
Syntax Format
super() super(type, object-or-type)
Parameter Description
- Parameter type:
- Type: class
- Description: The class for which to obtain the parent class proxy.
- Parameter object-or-type:
- Type: object or class
- Description: Must be an instance or subclass of type.
Function Description
- Return value: Returns a proxy object used to call methods of the parent class.
- Feature: Supports Method Resolution Order (MRO) and correctly handles multiple inheritance.
Examples
Example 1: Calling the Parent Class Constructor in a Subclass
Example
# Parent class
class Animal:
def __init__(self, name):
self.name = name
print(f"Animal {self.name} was born")
def speak(self):
print("Animal makes a sound")
# Subclass
class Dog(Animal):
def __init__(self, name, breed):
# Call the parent class __init__
super().__init__(name)
self.breed = breed
print(f"The breed is {self.breed}")
def speak(self):
# Call the parent class method
super().speak()
print("Woof!")
# Test
dog = Dog("Little White", "Husky")
# Output:
# Animal Little White was born
# The breed is Husky
dog.speak()
# Output:
# Animal makes a sound
# Woof!
class Animal:
def __init__(self, name):
self.name = name
print(f"Animal {self.name} was born")
def speak(self):
print("Animal makes a sound")
# Subclass
class Dog(Animal):
def __init__(self, name, breed):
# Call the parent class __init__
super().__init__(name)
self.breed = breed
print(f"The breed is {self.breed}")
def speak(self):
# Call the parent class method
super().speak()
print("Woof!")
# Test
dog = Dog("Little White", "Husky")
# Output:
# Animal Little White was born
# The breed is Husky
dog.speak()
# Output:
# Animal makes a sound
# Woof!
Expected output:
The animal Xiaobai was born Breed: Husky The animal makes sounds Woof woof!
Code analysis:
super().__init__(name)Calls the parent class constructor.super().speak()Calls the parent class method.- This allows extending the parent class functionality in the subclass.
Example 2: Multiple Inheritance
Example
# Multiple inheritance example
class A:
def method(self):
print("A.method")
class B(A):
def method(self):
print("B.method")
super().method() # Call A.method
class C(A):
def method(self):
print("C.method")
super().method() # Call A.method
class D(B, C):
def method(self):
print("D.method")
super().method() # Call in MRO order
# MRO: D -> B -> C -> A
d = D()
d.method()
# Output:
# D.method
# B.method
# C.method
# A.method
# View MRO
print(D.__mro__)
class A:
def method(self):
print("A.method")
class B(A):
def method(self):
print("B.method")
super().method() # Call A.method
class C(A):
def method(self):
print("C.method")
super().method() # Call A.method
class D(B, C):
def method(self):
print("D.method")
super().method() # Call in MRO order
# MRO: D -> B -> C -> A
d = D()
d.method()
# Output:
# D.method
# B.method
# C.method
# A.method
# View MRO
print(D.__mro__)
Expected output:
D.method B.method C.method A.method (<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)
Code analysis:
super()Called according to MRO (Method Resolution Order).- This ensures correctness of multiple inheritance.
Example 3: Without Parameters (Recommended)
Example
# Python 3 recommended usage: no parameters needed
class Parent:
def greet(self, name):
return f"Hello, {name}!"
class Child(Parent):
def greet(self, name):
# Call parent class method and extend
message = super().greet(name)
return message + " Welcome back!"
child = Child()
print(child.greet("Tom"))
# Output: Hello, Tom! Welcome back!
class Parent:
def greet(self, name):
return f"Hello, {name}!"
class Child(Parent):
def greet(self, name):
# Call parent class method and extend
message = super().greet(name)
return message + " Welcome back!"
child = Child()
print(child.greet("Tom"))
# Output: Hello, Tom! Welcome back!
Expected output:
Running in an actual environment will display the extended greeting.
super()It is a key function for implementing class inheritance and polymorphism in Python.
Other Extensions