📘 Lesson  ·  Lesson 87

Multiple Inheritance & MRO

Multiple Inheritance & MRO

Multiple Inheritance

💡 At a Glance

Unlike Java, Python allows a class to inherit from multiple parents. The MRO (Method Resolution Order) decides which parent's method runs first.

Example

Python
class A:
    def show(self): print("A")
class B(A):
    def show(self): print("B")
class C(A):
    def show(self): print("C")
class D(B, C):    # multiple inheritance
    pass

d = D()
d.show()          # follows MRO: D -> B -> C -> A
print(D.__mro__)
B (<class 'D'>, <class 'B'>, <class 'C'>, <class 'A'>, <class 'object'>)

Summary

  • Python allows multiple inheritance (D from B and C).
  • MRO decides method order: check with ClassName.__mro__.

Multiple Inheritance

💡 एक नज़र में

Java के उलट, Python एक class को कई parents से inherit करने देता है। MRO (Method Resolution Order) तय करता है कि कौन से parent का method पहले चले।

Example

Python
class A:
    def show(self): print("A")
class B(A):
    def show(self): print("B")
class C(A):
    def show(self): print("C")
class D(B, C):    # multiple inheritance
    pass

d = D()
d.show()          # MRO follow: D -> B -> C -> A
print(D.__mro__)
B (<class 'D'>, <class 'B'>, <class 'C'>, <class 'A'>, <class 'object'>)

सारांश

  • Python multiple inheritance देता है (D, B और C से)।
  • MRO method order तय करता है: ClassName.__mro__ से देखें।
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n

💻 Live Code Editor

Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें. Agar load na ho to naye tab mein kholें.