KEMBAR78
Python For Og Lecture 84 and 85 - Oop Mro and Polymorphism | PDF | Class (Computer Programming) | Method (Computer Programming)
0% found this document useful (0 votes)
17 views8 pages

Python For Og Lecture 84 and 85 - Oop Mro and Polymorphism

The document discusses Python object-oriented programming concepts including method resolution order (MRO), polymorphism, and method overriding. It provides examples of defining classes with inheritance relationships and demonstrates how MRO determines which method gets called and how a child class can override a method from its parent class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views8 pages

Python For Og Lecture 84 and 85 - Oop Mro and Polymorphism

The document discusses Python object-oriented programming concepts including method resolution order (MRO), polymorphism, and method overriding. It provides examples of defining classes with inheritance relationships and demonstrates how MRO determines which method gets called and how a child class can override a method from its parent class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

2/3/2021 Python for O&G Lecture 84 and 85: OOP - MRO and Polymorphism - Colaboratory

Python for Oil and Gas

Website - https://petroleumfromscratchin.wordpress.com/

LinkedIn - https://www.linkedin.com/company/petroleum-from-scratch

YouTube - https://www.youtube.com/channel/UC_lT10npISN5V32HDLAklsw

Method Resolution Order (MRO)

# Every class has it's own MRO

class Reservoir:
def __init__(self, av_por, av_perm, location):
self.porosity = av_por
self.permeability = av_perm
self.location = location

def describe(self):
print(f"Porosity: {self.porosity} and Permeability: {self.permeability} md") /
2/3/2021 Python for O&G Lecture 84 and 85: OOP - MRO and Polymorphism - Colaboratory

def situated(self):
print(f"This reservoir is located in {self.location}.")

class Any_class(Reservoir):
def __init__(self, av_por, av_perm, location, pi, pb):

super().__init__(av_por, av_perm, location)

self.initial = pi
self.bubble = pb

def pressure(self):
return f'Initial pressure: {self.initial} psi and bubble point pressure: {self.bubble}'

class Raj_res(Any_class):

def __init__(self, av_por, av_perm, location, pi, pb, s_w):


super().__init__(av_por, av_perm, location, pi, pb)

self.water_sat = s_w

def sat(self):
return f'water saturation is {self.water_sat}'

a = Reservoir(0.14,46, 'Kakinada')
b = Any_class(0.25, 65, 'Jaisalmer', 3241, 2540)
c = Raj_res(0.29, 57, 'Johrat', 2699, 2990, 0.26)

# to see MRO we use help function and class name

# help(class_name)

help(Raj_res)

/
2/3/2021 Python for O&G Lecture 84 and 85: OOP - MRO and Polymorphism - Colaboratory

Help on class Raj_res in module __main__:

class Raj_res(Any_class)
| Method resolution order:
| Raj_res
| Any_class
| Reservoir
| builtins.object
|
| Methods defined here:
|
| __init__(self, av_por, av_perm, location, pi, pb, s_w)
| Initialize self. See help(type(self)) for accurate signature.
|
| sat(self)
|
| ----------------------------------------------------------------------
| Methods inherited from Any_class:
|
| pressure(self)
|
| ----------------------------------------------------------------------
| Methods inherited from Reservoir:
|
| describe(self)
|
| situated(self)
|
| ----------------------------------------------------------------------
| Data descriptors inherited from Reservoir:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)

help(Reservoir)

Help on class Reservoir in module __main__:

class Reservoir(builtins.object)
| Methods defined here:
|
| __init__(self, av_por, av_perm, location) /
2/3/2021 Python for O&G Lecture 84 and 85: OOP - MRO and Polymorphism - Colaboratory
| Initialize self. See help(type(self)) for accurate signature.
|
| describe(self)
|
| situated(self)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)

Lecture 85: Polymorphism

Method Overriding

# Polymorphism - having multiple forms of a single thing

class Reservoir:
def __init__(self, av_por, av_perm, location):
self.porosity = av_por
self.permeability = av_perm
self.location = location

def describe(self):
print(f"Porosity: {self.porosity} and Permeability: {self.permeability} md")

def situated(self):
/
print(f"This reservoir is located in {self.location}.")
2/3/2021 Python for O&G Lecture 84 and 85: OOP - MRO and Polymorphism - Colaboratory
print(f This reservoir is located in {self.location}. )

class Any_class(Reservoir):
def __init__(self, av_por, av_perm, location, pi, pb):

super().__init__(av_por, av_perm, location)

self.initial = pi
self.bubble = pb

def pressure(self):
return f'Initial pressure: {self.initial} psi and bubble point pressure: {self.bubble}'

class Raj_res(Any_class):

def __init__(self, av_por, av_perm, location, pi, pb, s_w):


super().__init__(av_por, av_perm, location, pi, pb)

self.water_sat = s_w

def sat(self):
return f'water saturation is {self.water_sat}'

def describe(self):
return 'kjfbdsksds'

d = Reservoir(0.14, 52, 'Ahmedabad')


e = Raj_res(0.35, 74, 'Mehsana', 5000, 4500, 0.47) /
2/3/2021 Python for O&G Lecture 84 and 85: OOP - MRO and Polymorphism - Colaboratory

d.describe()

Porosity: 0.14 and Permeability: 52 md

e.describe()

'kjfbdsksds'

help(Raj_res)

Help on class Raj_res in module __main__:

class Raj_res(Any_class)
| Method resolution order:
| Raj_res
| Any_class
| Reservoir
| builtins.object
|
| Methods defined here:
|
| __init__(self, av_por, av_perm, location, pi, pb, s_w)
| Initialize self. See help(type(self)) for accurate signature.
|
| describe(self)
|
| sat(self)
|
| ----------------------------------------------------------------------
| Methods inherited from Any_class:
|
| pressure(self)
|
| ----------------------------------------------------------------------
| Methods inherited from Reservoir:
|
| situated(self)
|
| ----------------------------------------------------------------------
| Data descriptors inherited from Reservoir:
|
/
2/3/2021 Python for O&G Lecture 84 and 85: OOP - MRO and Polymorphism - Colaboratory
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)

print(6*3)

18

print('om'*3)

omomom

isinstance method

# checks whether our instance (object) of that class or not

# always returns True or False (Boolen vales)

# isinstance(object, class)

isinstance(e, Raj_res)

True

issubclass method

# issubclass(subclass, class)

issubclass(Any_class, Raj_res)
/
2/3/2021 Python for O&G Lecture 84 and 85: OOP - MRO and Polymorphism - Colaboratory

False

# NOTE:

# In multilevel inheritence, child class (derived class) is inherited from a single parent class (base class)

# when any class is derived from multiple classes at once, it is known as multiple innheritence

# Multilevel Inherit # Multiple Iheritence

# B drived from A # C derived from A & B


# C derived from B

# Multiple Inheritence makes our code quite complex and hence is not widely used

You might also like