Python Create a Class with Multiple Inheritance
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
)
:
(
"This is method1 from Parent1"
)
class
Parent2:
def
method2
(
self
)
:
(
"This is method2 from Parent2"
)
class
Child
(
Parent1
,
Parent2
)
:
def
method3
(
self
)
:
(
"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:
Parent1andParent2are two parent classes, each definingmethod1andmethod2method.Childclass inherits fromParent1andParent2, and defines its ownmethod3method.- in
Childinstance of the class can callParent1andParent2's method, and can also callChildclass's own method.
Output result:
This is method1 from Parent1
This is method2 from Parent2
This is method3 from Child
Python3 Examples