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

Python For Og Lecture 79 and 80 - Oop Part 5 Encapsulation Abstraction and Name Mangling

The document discusses object-oriented programming concepts like encapsulation, abstraction, and name mangling in Python. Encapsulation means grouping data and methods together in a class. Abstraction hides complexity from the user. Name mangling changes private attribute names to prevent accidental access.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python For Og Lecture 79 and 80 - Oop Part 5 Encapsulation Abstraction and Name Mangling

The document discusses object-oriented programming concepts like encapsulation, abstraction, and name mangling in Python. Encapsulation means grouping data and methods together in a class. Abstraction hides complexity from the user. Name mangling changes private attribute names to prevent accidental access.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

2/3/2021 Python for O&G Lecture 79 and 80: OOP - Part 5 - Encapsulation, Abstraction and Name Mangling - 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

Encapsulation & Abstraction

# ENCAPSULATION

# It just means all the data and all methods to be used with data are at one place

class Scores:
def __init__(self, res_score, drill_score, prod_score):
# instance variables
self.score_reservoir = res_score
Saved successfully!
self.score_drilling = drill_score
self.score_production = prod_score

/
def avg(self):
2/3/2021 Python for O&G Lecture 79 and 80: OOP - Part 5 - Encapsulation, Abstraction and Name Mangling - Colaboratory
def avg(self):
return {(self.score_reservoir + self.score_drilling + self.score_production)/3}

def maximum(self):
return max(self.score_reservoir, self.score_drilling, self.score_production)

# this is an example of encapsulation only. All the required things are at a single place. Encapsulated

# Abstraction

# ABSTRACTION MEANS HIDING THE COMPLEXITY OF CODE FROM USER

por = [0.1, 0.4, 0.35, 0.29]

# lets say we want to append a new porosity value

por.append(0.25)

print(por)

[0.1, 0.4, 0.35, 0.29, 0.25]

# Encapsulation and Abstraction are two fundamentals of OOP

Private and public methods/variables

# in many programming languages for example java and c++ there are some private methods and variables and some are public

# BUT IN PYTHON NOTHING IS PRIVATE. EVERYTHING IS PUBLIC

# but sometimes we give our code to someone else. We want them to not change certain things
# we can use _name instead of name to let them know that this is private to me, please don't change it
Saved successfully!

# This is just a convention not a rule.


/
2/3/2021 Python for O&G Lecture 79 and 80: OOP - Part 5 - Encapsulation, Abstraction and Name Mangling - Colaboratory

# for ex: this was our original code

class Well:
mw = 11.2
def __init__(self, compl_type, zones, depth):
self.type_of_completion = compl_type
self.num_of_zones = zones
self.depth_of_well = depth

def press_calc(self):
return 0.052*Well.mw*self.depth_of_well

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

well_a.type_of_completion

'open'

# to hint them private property:

class Well2:
mw = 11.2
def __init__(self, compl_type, zones, depth):
self._type_of_completion = compl_type # just to indicate please treat this as a private property
self.num_of_zones = zones
self.depth_of_well = depth

def press_calc(self):
return 0.052*Well.mw*self.depth_of_well

well_b = Well2('open', 1, 2500)

well_b._type_of_completion

Saved successfully!

/
2/3/2021 Python for O&G Lecture 79 and 80: OOP - Part 5 - Encapsulation, Abstraction and Name Mangling - Colaboratory

'open'

Lecture 80 - Name Mangling

Name Mangling (__name) ->> This is not a convention

# new class Well_2

class Well3:
mw = 12.1
def __init__(self, compl_type, zones, depth):
self.__type_of_completion = compl_type
self.num_of_zones = zones
self.depth_of_well = depth

def press_calc(self):
return 0.052*Well3.mw*self.depth_of_well

well_d = Well3('slotted liner', 4, 3900)

well_d.__type_of_completion

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-13-1c335acee91e> in <module>()
14 well_d = Well3('slotted liner', 4, 3900)
15
---> 16 well_d.__type_of_completion

AttributeError: 'Well3' object has no attribute '__type_of_completion'

SEARCH STACK OVERFLOW

well_d.__dict__
Saved successfully!

/
2/3/2021 Python for O&G Lecture 79 and 80: OOP - Part 5 - Encapsulation, Abstraction and Name Mangling - Colaboratory

# why python does this?, so that this variable remains associated with this class only.
# we'll understand this in INHERITENCE

{'_Well3__type_of_completion': 'slotted liner',


'depth_of_well': 3900,
'num_of_zones': 4}

Saved successfully!

You might also like