Sobes.tech
Junior — Middle

Is it possible to implement multiple inheritance in Python and how does it work?

sobes.tech AI

Answer from AI

In Python, multiple inheritance is implemented directly — a class can inherit from multiple base classes, listed separated by commas.

Example:

class A:
    def method_a(self):
        return "A"

class B:
    def method_b(self):
        return "B"

class C(A, B):
    pass

obj = C()
print(obj.method_a())  # Outputs: A
print(obj.method_b())  # Outputs: B

The operation of multiple inheritance in Python is managed by the Method Resolution Order (MRO) algorithm, which determines the order in which methods and attributes are searched in the class hierarchy. MRO considers the order of base classes and ensures predictable behavior when calling methods, especially if base classes have methods with the same names.

Is it possible to implement multiple inheritance in… - sobes.tech