Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

refactor(encapsulation-3.py): Add encapsulation and applies srp #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions 06.Encapsulation/encapsulation-3.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,33 @@ def set_val(self, val):
self.val = val

def get_val(self):
print(self.val)
return self.val

def increment_val(self):
self.val = self.val + 1
print(self.val)
self.set_val(self.get_val()+1)


a = MyInteger()
a.set_val(10)
a.get_val()
print(a.get_val())
a.increment_val()
print(a.get_val())
print("Before Break")

# Trying to break encapsulation in a new instance with an int
c = MyInteger()
c.val = 15
c.get_val()
print(c.get_val())
c.increment_val()
print(c.get_val())
print("After Break")

# Trying to break encapsulation in a new instance with a str
b = MyInteger()
b.val = "MyString" # <== Breaking encapsulation, works fine
b.get_val() # <== Prints the val set by breaking encap
print(b.get_val()) # <== Prints the val set by breaking encap
b.increment_val() # This will fail, since str + int wont work
print(b.get_val())
print("Changing DataType")
'''
O/P-
Expand All @@ -65,4 +67,4 @@ def increment_val(self):
Traceback (most recent call last):
self.val = self.val + 1
TypeError: can only concatenate str (not "int") to str
'''
'''