Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7cb55c0

Browse files
Encapsulation
1 parent faa1290 commit 7cb55c0

File tree

4 files changed

+150
-1
lines changed

4 files changed

+150
-1
lines changed

‎06.Encapsulation/encapsulation-1.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#encapsulation-1.py
2+
3+
# Encapsulation means to preserve data in classes using methods
4+
# Here, we're setting the 'val' attribute through 'set_val()'.
5+
# See the next example, `encapsulation-2.py` for more info
6+
7+
# In this example, we have two methods, `set_val` and `get_val`.
8+
# The first one sets the `val` value while the second one
9+
# prints/returns the value.
10+
11+
12+
class MyClass(object):
13+
def set_val(self, val):
14+
self.value = val
15+
16+
def get_val(self):
17+
# print(self.value)
18+
return self.value
19+
20+
21+
a = MyClass()
22+
b = MyClass()
23+
24+
a.set_val(10)
25+
b.set_val(100)
26+
27+
a.get_val()
28+
b.get_val()
29+
30+
# NOTE: If you run this code, it won't print anything to the screen.
31+
# This is because, even if we're calling `a.get_val()` and `b.get_val()`,
32+
# the `get_val()` function doesn't contain a `print()` function.
33+
# If we want to get the output printed to screen, we should do any of
34+
# the following:
35+
36+
# a) Either replace `return self.value` with `print(self.value)`
37+
# or add a print statement **above** `return` as `print(self.value)`.
38+
39+
# b) Remove `return(self.value)` and replace it with `print(self.value)`

‎06.Encapsulation/encapsulation-2.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#encapsulation-2.py
2+
3+
# This example builds on top of `encapsulation-1.py`.
4+
# Here we see how we can set values in methods without
5+
# going through the method itself, ie.. how we can break
6+
# encapsulation.
7+
8+
# NOTE: BREAKING ENCAPSULATION IS BAD.
9+
10+
11+
class MyClass(object):
12+
def set_val(self, val):
13+
self.value = val
14+
15+
def get_val(self):
16+
print(self.value)
17+
18+
19+
a = MyClass()
20+
b = MyClass()
21+
22+
a.set_val(10)
23+
b.set_val(1000)
24+
a.value = 100 # <== Overriding `set_value` directly
25+
# <== ie.. Breaking encapsulation
26+
27+
a.get_val()
28+
b.get_val()
29+
30+
'''
31+
O/P-
32+
100
33+
1000
34+
'''

‎06.Encapsulation/encapsulation-3.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#encapsulation-3.py
2+
3+
# Here we look at another example, where we have three methods
4+
# set_val(), get_val(), and increment_val().
5+
6+
# set_val() helps to set a value, get_val() prints the value,
7+
# and increment_val() increments the set value by 1.
8+
9+
# We can still break encapsulation here by calling 'self.value'
10+
# directly in an instance, which is **BAD**.
11+
12+
# set_val() forces us to input an integer, ie.. what the code wants
13+
# to work properly. Here, it's possible to break the encapsulation by
14+
# calling 'self.val` directly, which will cause unexpected results later.
15+
# In this example, the code is written to enforce an intger as input, if we
16+
# don't break encapsulation and go through the gateway 'set_val()'
17+
18+
#
19+
20+
21+
class MyInteger(object):
22+
def set_val(self, val):
23+
try:
24+
val = int(val)
25+
except ValueError:
26+
return
27+
self.val = val
28+
29+
def get_val(self):
30+
print(self.val)
31+
32+
def increment_val(self):
33+
self.val = self.val + 1
34+
print(self.val)
35+
36+
37+
a = MyInteger()
38+
a.set_val(10)
39+
a.get_val()
40+
a.increment_val()
41+
print("Before Break")
42+
43+
# Trying to break encapsulation in a new instance with an int
44+
c = MyInteger()
45+
c.val = 15
46+
c.get_val()
47+
c.increment_val()
48+
print("After Break")
49+
50+
# Trying to break encapsulation in a new instance with a str
51+
b = MyInteger()
52+
b.val = "MyString" # <== Breaking encapsulation, works fine
53+
b.get_val() # <== Prints the val set by breaking encap
54+
b.increment_val() # This will fail, since str + int wont work
55+
print("Changing DataType")
56+
'''
57+
O/P-
58+
10
59+
11
60+
Before Break
61+
15
62+
16
63+
After Break
64+
MyString
65+
Traceback (most recent call last):
66+
self.val = self.val + 1
67+
TypeError: can only concatenate str (not "int") to str
68+
'''

‎README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
02. [Methods-Functions](https://github.com/Shikha-code36/Object-Oriented-Programming-OOPs-Python#02-methodsfunctions)
66
03. [Objects](https://github.com/Shikha-code36/Object-Oriented-Programming-OOPs-Python#03-objects)
77
04. [Constructors](https://github.com/Shikha-code36/Object-Oriented-Programming-OOPs-Python#04-constructors)
8-
05. [Inheritance](https://github.com/Shikha-code36/Object-Oriented-Programming-OOPs-Python#04-constructors)
8+
05. [Inheritance](https://github.com/Shikha-code36/Object-Oriented-Programming-OOPs-Python#05-inheritance)
9+
06. [Encapsulation](https://github.com/Shikha-code36/Object-Oriented-Programming-OOPs-Python#06-encapsulation)
910

1011
------------
1112
## What do you understand by OOPs?
@@ -131,4 +132,11 @@ ex- Man Inheriting features from his father
131132

132133
[Detailed Explanation](05.Inheritance)
133134

135+
------------
136+
#### 06. Encapsulation
137+
138+
Encapsulation involves the bundling of data members and functions inside a single class. Bundling similar data members and functions inside a class also helps in data hiding. Encapsulation also ensures that objects are self-sufficient functioning pieces and can work independently.
139+
140+
[Detailed Explanation](06.Encapsulation)
141+
134142
------------

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /