Python super() Function
super() is a built-in function in Python used to call methods of a parent (super) class.
In class inheritance,super() lets you call methods of the parent class, enabling code reuse and method extension. It is a very important function in Python object-oriented programming.
Word meaning: super means "super", here referring to the parent or super class.
Basic syntax and parameters
Syntax format
super()
super(type, object-or-type)
Parameter Description
- Parameter type:
- Type: class
- Description: the class whose parent Proxy is wanted.
- 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: It supports the method resolution order (MRO) and handles multiple inheritance correctly.
Example
Example 1: calling the parent constructor in a subclass
Example
Parent Class
class
Animal:
def
init
(
self
,
name
)
:
self
.
name
=
name
(
f
"Animal {self.name} was born"
)
def
speak
(
self
)
:
(
"The animal makes a sound"
)
Subclass
class
Dog
(
Animal
)
:
def
init
(
self
,
name
,
breed
)
:
Call the parent class's init
super
(
)
.
init
(
name
)
self
.
breed
=
breed
(
f
"Breed is {self.breed}"
)
def
speak
(
self
)
:
Call a method of the parent class
super
(
)
.
speak
(
)
(
"Woof!"
)
Testing
dog
=
Dog
(
"Xiaobai"
,
"Husky"
)
Output:
Animal Xiaobai was born
Breed is Husky
dog.
speak
(
)
Output:
The animal makes a sound
Woof!
Expected output:
动物 小白 出生了
品种是 哈士奇
动物发出声音
汪汪!
Code analysis:
super().__init__(name)calls the constructor of the parent class.super().speak()calls the methods of the parent class.- This lets a subclass extend the functionality of its parent class.
Example 2: multiple inheritance
Example
Multiple inheritance example
class
A:
def
method
(
self
)
:
(
"A.method"
)
class
B
(
A
)
:
def
method
(
self
)
:
(
"B.method"
)
super
(
)
.
method
(
)
Call A.method
class
C
(
A
)
:
def
method
(
self
)
:
(
"C.method"
)
super
(
)
.
method
(
)
Call A.method
class
D
(
B
,
C
)
:
def
method
(
self
)
:
(
"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 the MRO
(
D.mro
)
Expected output:
D.method
B.method
C.method
A.method
(, , , , )
Code analysis:
super()It calls methods according to the MRO (method resolution order).- This ensures that multiple inheritance behaves correctly.
Example 3: without arguments (recommended)
Example
Recommended usage in Python 3: no arguments needed
class
Parent:
def
greet
(
self
,
name
)
:
return
f
"Hello, {name}!"
class
Child
(
Parent
)
:
def
greet
(
self
,
name
)
:
Call the parent method and extend it
message
=
super
(
)
.
greet
(
name
)
return
message +
" Welcome back!"
child
=
Child
(
)
(
child.
greet
(
"Tom"
)
)
Output: Hello, Tom! Welcome back!
Expected output:
Running it in a real environment shows the extended greeting.
super() is a key function for implementing class inheritance and polymorphism in Python.
Python3 built-in function