0% found this document useful (0 votes)
65 views23 pages

Lecture-46 (Multiple, Hybrid Inheritance)

This document discusses multiple inheritance in Python. It defines multiple inheritance as a class being derived from more than one base class, allowing the derived class to inherit features from all base classes. It provides an example of a ScienceStudent class that inherits from both Person and Student classes. It also discusses the Method Resolution Order (MRO) algorithm that Python uses to determine the order of method lookup when multiple inheritance is used. The "diamond problem" of ambiguous inheritance is also described.

Uploaded by

Ritika Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views23 pages

Lecture-46 (Multiple, Hybrid Inheritance)

This document discusses multiple inheritance in Python. It defines multiple inheritance as a class being derived from more than one base class, allowing the derived class to inherit features from all base classes. It provides an example of a ScienceStudent class that inherits from both Person and Student classes. It also discusses the Method Resolution Order (MRO) algorithm that Python uses to determine the order of method lookup when multiple inheritance is used. The "diamond problem" of ambiguous inheritance is also described.

Uploaded by

Ritika Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

PYTHON

LECTURE 46
Today’s Agenda

• Advance Concepts Of Object Oriented


Programming-IV

• Multiple Inheritance
• The MRO Algorithm
• Hybrid Inheritance
• The Diamond Problem
Multiple Inheritance

 Like C++, in Python also a class  can be derived from


more than one base class.

 This is called multiple inheritance.

 In multiple inheritance, the features of all the base


classes are inherited into the derived class. 
Multiple Inheritance
Syntax

class A:
# properties of class A

class B:
#properties of class B

class C(A,B):
# class C inheriting property of class A
# class C inheriting property of class B
# more properties of class C
Example

class Person: class ScienceStudent(Person,Student):


def __init__(self,name,age):
def __init__(self,name,age,roll,per,stream):
self.name=name Person.__init__(self,name,age)
self.age=age Student.__init__(self,roll,per)
def getname(self): self.stream=stream
def getstream(self):
return self.name
return self.stream
def getage(self):
return self.age ms=ScienceStudent("Suresh",19,203,89.4,"maths")
class Student: print("Name:",ms.getname())
print("Age:",ms.getage())
def __init__(self,roll,per):
print("Roll:",ms.getroll())
self.roll=roll print("Per:",ms.getper())
self.per=per print("Stream:",ms.getstream())
def getroll(self):
return self.roll Output:
def getper(self):
return self.per
Guess The Output ?

class A:
Output:
def m(self):
print("m of A called")

Why did m( ) of A
class B: got called ?
def m(self):
print("m of B called") This is because of a
special rule in
Python called MRO
class C(A,B):
pass

obj=C()
obj.m()
What Is MRO In Python ?

 In languages that use multiple inheritance, the order in


which base classes are searched when looking for a
method is often called the Method Resolution Order,
or MRO.

 MRO RULE :
 In the multiple inheritance scenario, any specified attribute is searched
first in the current class. If not found, the search continues into
parent classes, left-right fashion and then in depth-first
without searching same class twice.
Can We See This MRO ?

 Yes, Python allows us to see this MRO by calling a method


called mro( ) which is present in every class by default.
Example

class A:
def m(self):
print("m of A called")

class B:
def m(self):
print("m of B called")

class C(A,B):
pass
print(C.mro())
Output
Another Way To See MRO ?

 There is a tuple also called __mro__ made available in


every class by Python using which we can get the same
output as before
Example

class A:
def m(self):
print("m of A called")

class B:
def m(self):
print("m of B called")

class C(A,B):
pass
print(C.__mro__)
Output
The Hybrid Inheritance

 This form combines more than one form of inheritance.


Basically, it is a blend of more than one type of inheritance.
Example

class A: obj=D()
def m1(self): obj.m1()
print("m1 of A called") obj.m2()
class B(A): obj.m3()
def m2(self):
print("m2 of B called")
class C(A):
def m3(self):
print("m3 of C called")
class D(B,C):
pass
Output:
The Diamond Problem

 The “diamond problem” is the generally used term for


an ambiguity that arises in hybrid inheritance .

 Suppose two classes B and C inherit from a superclass A,


and another class D inherits from both B and C.

 If there is a method "m" in A that B and C have


overridden, then the question is which version of the
method does D inherit?
Guess The Output

class A:
obj=D()
def m(self):
obj.m()
print("m of A called")

class B(A):
Why m() of B was called ?
def m(self):
As discussed previously , Python
print("m of B called")
uses MRO to search for an
attribute which goes from left to
class C(A): right and then in depth first.
def m(self): Now since B is the first
print("m of C called") inherited class of D so Python
called m( ) of B
class D(B,C):
pass
Output:
Guess The Output

class A:
obj=D()
def m(self):
obj.m()
print("m of A called")

class B(A):
def m(self):
print("m of B called")

class C(A):
def m(self):
print("m of C called")

class D(C,B):
pass
Output:
Guess The Output

class A:
obj=D()
def m(self): obj.m()
print("m of A called")
Why m() of C was called ?
class B(A):
pass MRO goes from left to right first
and then depth first. In our case
Python will look for method m()
class C(A): in B but it won’t find it there .
def m(self): Then it will search m() in C
print("m of C called") before going to A. Since it finds
m() in C, it executes it dropping
the further search
class D(B,C):
pass
Output:
Guess The Output

class A:
obj=D()
def m(self):
print("m of A called")
obj.m()

class B(A):
def m(self):
print("m of B called")

class C(A):
def m(self):
print("m of C called")

class D(B,C):
def m(self):
print("m of D called")
Output:
Guess The Output

class A:
def m(self): obj=D()
print("m of A called") obj.m()
class B(A):
def m(self):
print("m of B called")

class C(A):
def m(self):
print("m of C called")

class D(B,C):
def m(self):
print("m of D called")
B.m(self)
C.m(self)
A.m(self)
Output:
Guess The Output

class A:
def m(self): obj=D()
print("m of A called") obj.m()
class B(A):
def m(self):
print("m of B called") Why m() of A was called
A.m(self)
class C(A):
twice?
def m(self): This is because we have
print("m of C called") called A.m(self) in both B
A.m(self)
class D(B,C):
and C classes due to which
def m(self): the method m() of A gets
print("m of D called") called 2 times
B.m(self)
C.m(self)
Output:
Using super( ) To Solve
The Previous Problem

 In the previous code the method m( ) of A was getting


called twice.

 To resolve this problem we can use super( ) function to


call m() from B and C .

 As previously mentioned Python follows MRO and never


calls same method twice so it will remove extra call to
m( ) of A and will execute m() only once
Guess The Output

class A:
def m(self): obj=D()
print("m of A called") obj.m()
class B(A):
def m(self):
print("m of B called")
super().m()
class C(A):
def m(self):
print("m of C called")
super().m()
class D(B,C):
def m(self):
print("m of D called")
super().m()
Output:

You might also like