0% found this document useful (0 votes)
16 views4 pages

Python For Og Lecture 78 - Oop Part 4

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)
16 views4 pages

Python For Og Lecture 78 - Oop Part 4

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

2/3/2021 Python for O&G Lecture 78: OOP Part 4 - 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 Variable

class Circle:

def __init__(self, pi, r):

self.pi = pi
self.radius = r

def area(self):
return self.pi*(self.radius)**2

def circum(self):
return 2*self.pi*self.radius

/
2/3/2021 Python for O&G Lecture 78: OOP Part 4 - Colaboratory

c_1 = Circle(3.14, 2)
c_2 = Circle(3.14, 3)

c_1.area()

12.56

c_2.circum()

18.84

# class variable is that variable inside class which have a common value for all the objects representing that particular class

class Circle_2:

pi = 3.14 # class variable or attribute

def __init__(self, r):

self.radius = r

def area(self):
return Circle_2.pi*(self.radius)**2

def circum(self):
return 2*Circle_2.pi*self.radius

c_3 = Circle_2(2)

c_3.area()

/
2/3/2021 Python for O&G Lecture 78: OOP Part 4 - Colaboratory
12.56

c_4 = Circle_2(3)

c_4.circum()

18.84

# let's use this example

# we have different wells with different attributes

# let's say we want to create a method which calculates pressure at given well depth

# one more thing. We want to calculate pressures using same mud.

# mud is same so we need not separately require attribute insisde __init__

# we can make a class variable

class Well:

mw = 12 # ppg

def __init__(self, comp_type, zones, depth):

self.type_of_comp = comp_type
self.no_of_zones = zones
self.depth = depth

def pres_calc(self):
return 0.052*Well.mw*(self.depth)

well_1 = Well('open', 1, 2500)


/
2/3/2021 Python for O&G Lecture 78: OOP Part 4 - Colaboratory

well_1.pres_calc()

1560.0

well_1.__dict__

{'depth': 2500, 'no_of_zones': 1, 'type_of_comp': 'open'}

You might also like