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

Python Create a Class with Multiple Inheritance

Python Create a Class with Multiple Inheritance

Document Object Reference Manual Python3 Examples

In Python, multiple inheritance means a class can inherit from multiple parent classes. Through multiple inheritance, a subclass can inherit the attributes and methods of multiple parents. The following is a simple example showing how to create a class with multiple inheritance.

Example

class

Parent1:

   

def

method1

(

self

)

:

       

print

(

"This is method1 from Parent1"

)

class

Parent2:

   

def

method2

(

self

)

:

       

print

(

"This is method2 from Parent2"

)

class

Child

(

Parent1

,

Parent2

)

:

   

def

method3

(

self

)

:

       

print

(

"This is method3 from Child"

)

Create an instance of the Child class

child

=

Child

(

)

Call the method inherited from Parent1

child.

method1

(

)

Call the method inherited from Parent2

child.

method2

(

)

Call the Child class's own method

child.

method3

(

)

Code analysis:

Output result:

This is method1 from Parent1
This is method2 from Parent2
This is method3 from Child

Document Object Reference Manual Python3 Examples