0% found this document useful (0 votes)
29 views6 pages

Python For Og Lecture 76 and 77 - Oop Part 3

The document discusses Python object-oriented programming concepts like instance methods, creating methods for a Reservoir class, and using those methods. It also provides an example of creating a Scores class with methods for calculating average and maximum scores.
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)
29 views6 pages

Python For Og Lecture 76 and 77 - Oop Part 3

The document discusses Python object-oriented programming concepts like instance methods, creating methods for a Reservoir class, and using those methods. It also provides an example of creating a Scores class with methods for calculating average and maximum scores.
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/ 6

2/3/2021 Python for O&G Lecture 76 and 77 : OOP - Part 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

Instance methods (Object methods)

# see some methods of existing class

l = [1, 2, 3, 4, 5]

l.pop()

print(l)

[1, 2, 3, 4]

l.append('oil')
print(l) /
2/3/2021 Python for O&G Lecture 76 and 77 : OOP - Part 3 - Colaboratory

[1, 2, 3, 4, 'oil']

# Now, we know how to create class and objects

# now we'll see how we can create our own such methods

# I have created this class already in previous video

class Reservoir:

def __init__(self, por, perm, depth):

self.porosity = por
self.permeability = perm
self.depth_of_reservoir = depth

res_a = Reservoir(0.41, 50, 3200)

# now I want to create a method for my class which describes the properties of reservoir in a nice manner

class Reservoir_2:

def __init__(self, por, perm, depth):

self.porosity = por
self.permeability = perm
self.depth_of_reservoir = depth

def describe(self):
return f'Porosity of this reservoir is {self.porosity*100}% and permiability is {self.permeability} md. This reservoir is located at the depth of {self.dep

/
2/3/2021 Python for O&G Lecture 76 and 77 : OOP - Part 3 - Colaboratory

res_a = Reservoir(0.41, 50, 3200)

res_b = Reservoir_2(0.41, 50, 3200)

res_a.describe()

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-12-062bca49a4dc> in <module>()
----> 1 res_a.describe()

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

SEARCH STACK OVERFLOW

res_b.describe()

'Porosity of this reservoir is 41.0% and permiability is 50 md. This reservoir is located at the depth of 3200'

res_c = Reservoir_2(0.2, 89, 2500)


res_c.describe()

'Porosity of this reservoir is 20.0% and permiability is 89 md. This reservoir is located at the depth of 2500'

IMPORTANT POINT

# how this works in reality

# class_name.method_name(object_name)

Reservoir_2.describe(res_c)

'Porosity of this reservoir is 20.0% and permiability is 89 md. This reservoir is located at the depth of 2500'

/
a = [1, 2, 3, 4, 5]
2/3/2021 Python for O&G Lecture 76 and 77 : OOP - Part 3 - Colaboratory
a [1, 2, 3, 4, 5]

a.pop()

print(a)

[1, 2, 3, 4]

list.pop(a)

print(a)

[1, 2, 3]

list.append(a, 'oil') # self, attr1

print(a)

[1, 2, 3, 'oil']

Assignment 20

# create a class Scores with attributes scores of diff subjects

# create a method which calculates the averagae of all the subject's scores

# create another method which gives you max score out of three

Lecture 77: Solution - Assignment 20

/
class Scores:
2/3/2021 Python for O&G Lecture 76 and 77 : OOP - Part 3 - Colaboratory
class Scores:

def __init__(self, res, drill, prod):


self.reservoir_score = res
self.drilling_score = drill
self.production_score = prod

def avg(self):
return (self.reservoir_score + self.drilling_score + self.production_score)/3

def maximum(self):
return max(self.reservoir_score , self.drilling_score , self.production_score)

Mohit = Scores(75, 85, 90)

Shashank = Scores(78, 96, 48)

Mohit.avg()

83.33333333333333

Shashank.avg()

74.0

Mohit.maximum()

90

Shashank.maximum()

96

/
2/3/2021 Python for O&G Lecture 76 and 77 : OOP - Part 3 - Colaboratory

You might also like