Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python super() Function

Python super() Function

Python3 Built-in Functions Python3 built-in 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

- Type: class - Description: the class whose parent Proxy is wanted. - Type: object or class - Description: must be an instance or subclass of type.

Function description


Example

Example 1: calling the parent 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

(

"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

       

print

(

f

"Breed is {self.breed}"

)

   

def

speak

(

self

)

:

       

Call a method of the parent class

       

super

(

)

.

speak

(

)

       

print

(

"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:

  1. super().__init__(name) calls the constructor of the parent class.
  2. super().speak() calls the methods of the parent class.
  3. This lets a subclass extend the functionality of its parent class.

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 the MRO

print

(

D.mro

)

Expected output:

D.method
B.method
C.method
A.method
(, , , , )

Code analysis:

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

(

)

print

(

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 Functions Python3 built-in function