0% found this document useful (0 votes)
37 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)
37 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

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

Python for Oil and Gas

Website - [Link]

LinkedIn - [Link]

YouTube - [Link]

Instance methods (Object methods)

# see some methods of existing class

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

[Link]()

print(l)

[1, 2, 3, 4]

[Link]('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):

[Link] = por
[Link] = 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):

[Link] = por
[Link] = perm
self.depth_of_reservoir = depth

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

/
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]

[Link]()

print(a)

[1, 2, 3, 4]

[Link](a)

print(a)

[1, 2, 3]

[Link](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)

[Link]()

83.33333333333333

[Link]()

74.0

[Link]()

90

[Link]()

96

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

You might also like