Python Multiple Inheritance

📔 : Python 🔗

mostly ज्यादातर programming languages multiple inheritance को support नहीं करती है , लेकिन Python के साथ ऐसा नहीं है। Python Multiple Inheritance को easily handle करती है।


Multiple Inheritance में किसी class द्वारा एक से ज्यादा classes को Inherit किया जाता है। मतलब कोई class A, class B, C को एक साथ Inherit कर सकती है।


Multiple inheritance को आप नीचे दिए गए graph की help से समझ सकते हैं।

Python Multiple Inheritence

Multiple Classes को Inherit करने के लिए simply parenthesis में comma से separate करके classes के name pass कर दिए जाते हैं।

Python Multiple Inheritance Example

Copy Fullscreen Close Fullscreen Run
# class A.
class A :
  def classa_fun(self) :
    print('Function in class A')

# class B.
class B :
  def classb_fun(self) :
    print('Function in class B')

# class C that extends class A and B.
class C(A,B) :
  def classc_fun(self) :
    print('Function in class C')

# now we can access all the functions using class C object that are in class A and B.
obj = C()
obj.classa_fun()
obj.classb_fun()
obj.classc_fun()
Output
C:\Users\Rahulkumar\Desktop\python>python multiple_inhrt.py
Function in class A
Function in class B
Function in class C

Inheritance में इस बात का ध्यान रहे कि कोई class सिर्फ एक ही बार inherit की जा सकती यही। एक class को आप बार बार inherit नहीं कर सकते हैं।
For Example -

class B(A) :
  pass

class C (A, B) : #here we are inheriting class A again.
  pass 

It raises an error : 
Traceback (most recent call last):
  File "test.py", line 7, in <module>
    class C (A, B) :
TypeError: Cannot create a consistent method resolution
order (MRO) for bases A, B

Add __init__() Method

Multiple Inheritance में parent class का कोई method या property access करने के लिए हमेशा Parent Class Name use होता है।
For Example -

Copy Fullscreen Close Fullscreen Run
# class A.
class A :
  def __init__(self) :
    print('Constructor in class A')

# class B.
class B :
  def __init__(self) :
    print('Constructor in class B')

# class C extends class A and B.
class C(A,B) :
  def __init__(self) :
    A.__init__(self)
    B.__init__(self)
    print('Constructor in class C')

# just initialize the object.
obj = C()
Output
C:\Users\Rahulkumar\Desktop\python>python multiple_inhrt.py
Constructor in class A
Constructor in class B
Constructor in class C

Note : Example में parent class के __init__() method को access करने के लिए Class Name use किया गया है। और अगर हो सके तो multiple inheritance में super() method के through parent class का कुछ भी access न करें इससे आपको confusion होगा। क्योंकि super() method ,पहले pass की गयी class से ही data access करता है।
For Example -

class C(A,B) :
  def __init__(self) :
    super().__init__()
    print('Constructor in class C')

# just initialize the object.
obj = C()

Output :

Constructor in class A
Constructor in class C

example में class C(A,B) लिखा गया मतलब class A को super() method ज्यादा priority देगा। इसलिए Parent Class Name use करें जिससे कम से कम confusion रहेगा।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers