0% found this document useful (0 votes)
7 views

Python For Og Lecture 82 and 83 - Oop Inheritance Part 2 and 3

The document discusses inheritance in Python by defining classes for reservoirs. It shows how to inherit attributes and methods from a parent class to a child class. It demonstrates single inheritance with the Reservoir and Tight_reservoir classes, and multi-level inheritance with Reservoir, Any_class and Raj_res classes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Python For Og Lecture 82 and 83 - Oop Inheritance Part 2 and 3

The document discusses inheritance in Python by defining classes for reservoirs. It shows how to inherit attributes and methods from a parent class to a child class. It demonstrates single inheritance with the Reservoir and Tight_reservoir classes, and multi-level inheritance with Reservoir, Any_class and Raj_res classes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

2/3/2021 Python for O&G Lecture 82 and 83: Inheritance Part 2 and 3 - 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

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}.")

# class Tight_reservoir: /
2/3/2021 Python for O&G Lecture 82 and 83: Inheritance Part 2 and 3 - Colaboratory

# def __init__(self, av_por, av_perm, location, stimul):

# self.porosity = av_por
# self.permeability = av_perm
# self.location = location
# self.stimulation = stimul

# 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}.")

# let's shorten this code using Inheritence

# METHOD 1

# class Child class(Parent class):

# def __init__(self, all the atributes here):


# Parent class.__init__(attributes of parent class)

# then only define instance variable for child class

class Tight_reservoir(Reservoir):

def __init__(self, av_por, av_perm, location, stimul):


Reservoir.__init__(self, av_por, av_perm, location)

self.stimulation = stimul

res_a = Tight_reservoir(0.10, 11, 'Assam', 'Hydraulic fracturing')

res_a.location
/
2/3/2021 Python for O&G Lecture 82 and 83: Inheritance Part 2 and 3 - Colaboratory

'Assam'

res_a.describe()

Porosity: 0.1 and Permeability: 11 md

# method 2

# class Child class(Parent class):

# def __init__(self, all the atributes here):


#
# super().__init__(attributes of parent class) # no need to write self here

# then only define instance variable for child class

class Tight_reservoir(Reservoir):
def __init__(self, av_por, av_perm, location, stimul):
# Reservoir.__init__(self, av_por, av_perm, location)
super().__init__(av_por, av_perm, location)

self.stimulation = stimul

def stim_method(self):
return f'Stimulation method for this reservoir is {self.stimulation}'

res_c = Tight_reservoir(0.12, 9, 'Rajasthan', 'HF')

res_d = Reservoir(0.12, 15, 'Assam')

res_d.stim_method()

/
2/3/2021 Python for O&G Lecture 82 and 83: Inheritance Part 2 and 3 - Colaboratory

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-24-45f39720ba47> in <module>()
3 res_d = Reservoir(0.12, 15, 'Assam')
4
----> 5 res_d.stim_method()

AttributeError: 'Reservoir' object has no attribute 'stim_method'

Lecture 83: Multilevel Inheritance


SEARCH STACK OVERFLOW

n number of inheritence possible from one class


Multilevel Inheritence

# one more thing!

# we can inheit as many classes we want

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}.")

class Any_class(Reservoir):
def __init__(self, av_por, av_perm, location, pi, pb):
/
2/3/2021 Python for O&G Lecture 82 and 83: Inheritance Part 2 and 3 - Colaboratory

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}'

a = Any_class(0.26, 39, 'Maharashtra', 5000, 4000)

a.pressure()

'Initial pressure: 5000 psi and bubble point pressure: 4000'

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}'

b = Raj_res(0.14, 52, 'Assam', 4500, 4100, 0.3)

b.describe()

Porosity: 0.14 and Permeability: 52 md

# Reservoir - Grandfather
# Any_class - father
# Raj_res - child

/
2/3/2021 Python for O&G Lecture 82 and 83: Inheritance Part 2 and 3 - Colaboratory

You might also like