2/3/2021 Python for O&G Lecture 73, 74, 75: OOP - Part 1 and 2 - 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
# concept of OOP is in many popular languages. But syntax differsin each language
# It does not increase any functionality. OOP is just a style of writing easy and managable code.
# uses the concept of class, object and method
# python is an Object Oriented Programming (OOP) language
# class and object(instance), method --> three words you'll hear more now
# example
/
2/3/2021 Python for O&G Lecture 73, 74, 75: OOP - Part 1 and 2 - Colaboratory
flow_rate = [300, 150, 200, 1000] #bbl/day
alphabets = ['a', 'b', 'c', 'd'] # string
mix = [14, 59, 'python', 80.87] # random
list_1 = []
# all above are different objects but all belong to same class i.e. list
# example: Cricket match is a class. Test match, ODI and T20 matches are objects of my class Cricket
# method
flow_rate.append(450)
print(flow_rate)
[300, 150, 200, 1000, 450]
list_1.append('Divyansh')
print(list_1)
['Divyansh']
list_1.append('Sethi')
print(list_1)
['Divyansh', 'Sethi']
# methods are functions defined for a particualar class
# a method will perform a fix type of functionality on each object of a class
# So, strings tuples dictionaries and lists all these are already created classes. We have been working already with OOP
/
# I hope you get the idea behind OOP. If not don't worry, as we proeceed you'll get better at it
2/3/2021 Python for O&G Lecture 73, 74, 75: OOP - Part 1 and 2 - Colaboratory
# I hope you get the idea behind OOP. If not don t worry, as we proeceed you ll get better at it
Lecture 74: OOP - Part 2
Create your own class and objects
# STEP 1
# You always use class keyword and then name of class with first letter capital
# STEP 2
# then define a special method -> init method or constructor. SYNTAX - __init__(self, attr1, attr2, .....)
# self is the representation of the object
# STEP 3
# Create instance variables
# self.instance_variable1 = attr1
# self.instance_variable2 = attr2
# self.instance_variable3 = attr3
class Reservoir:
def __init__(self, por, perm, depth):
print('this works')
self.porosity = por
self.permeability = perm
self.depth_of_reservoir = depth
/
2/3/2021 Python for O&G Lecture 73, 74, 75: OOP - Part 1 and 2 - Colaboratory
# objects
res_a = Reservoir(0.14, 35, 4000)
this works
res_a.porosity
0.14
res_c.depth_of_reservoir
3655
# look at this explanation now
# whenever we call the class, first thing happens in __init__ method (constructor) get called
# (to prove this we can add any statement just after the __init__ method)
# self represents our object. MEANS __init__ takes always first input of self. So our object name doesn't matter here
# self.porosity means object.porosity which will give the porosity of our object
# self represents object1 and also object 2 and all other objects as well
# You can use any other term as well in place of self. But prefereed is self
Assignment 19
/
2/3/2021 Python for O&G Lecture 73, 74, 75: OOP - Part 1 and 2 - Colaboratory
# create a class 'Well' with attributes like completion type, no. of zones, depth of well
# create three instances (objects) of your class 'Well'
class Well:
def __init__(self, comp_type, zones, depth):
self.type_of_comp = comp_type
self.zones = zones
self.depth = depth
well_1 = Well('cased well', 3, 2500)
well_1.type_of_comp
'cased well'
well_1.depth
2500
well_2 = Well('open hole', 1, 1500)
well_2.zones
/
2/3/2021 Python for O&G Lecture 73, 74, 75: OOP - Part 1 and 2 - Colaboratory