Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Multiple Inheritance | Inheritance
In-Depth Python OOP

bookMultiple Inheritance

A class can be a subclass of a few other classes (inherit from multiple other classes).

To inherit from multiple classes, you can write the names of the classes separated by commas (,):

123456789101112131415161718192021
class First: first_attribute = "First" def first_method(self): print("The first_method from the First class") class Second: second_attribute = "Second" def second_method(self): print("The second_method from the Second class") class Child(First, Second): pass instance = Child() print(instance.first_attribute) print(instance.second_attribute) instance.first_method() instance.second_method()
copy

But the multiple inheritance has an order. The priority of the attributes/methods search is going from left to right:

1234567891011
class First: attribute = "First" class Second: attribute = "Second" class Child(Second, First): # Order starts from the `Second` pass instance = Child() print(instance.attribute)
copy

This demonstrates how the order of inheritance affects attribute resolution in cases where the same attribute is defined in multiple parent classes.

question mark

How to inherit a class from Parent1 and Parent2?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 3

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 2.78

bookMultiple Inheritance

Svep för att visa menyn

A class can be a subclass of a few other classes (inherit from multiple other classes).

To inherit from multiple classes, you can write the names of the classes separated by commas (,):

123456789101112131415161718192021
class First: first_attribute = "First" def first_method(self): print("The first_method from the First class") class Second: second_attribute = "Second" def second_method(self): print("The second_method from the Second class") class Child(First, Second): pass instance = Child() print(instance.first_attribute) print(instance.second_attribute) instance.first_method() instance.second_method()
copy

But the multiple inheritance has an order. The priority of the attributes/methods search is going from left to right:

1234567891011
class First: attribute = "First" class Second: attribute = "Second" class Child(Second, First): # Order starts from the `Second` pass instance = Child() print(instance.attribute)
copy

This demonstrates how the order of inheritance affects attribute resolution in cases where the same attribute is defined in multiple parent classes.

question mark

How to inherit a class from Parent1 and Parent2?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 3
some-alt