Last active
March 4, 2019 13:31
-
-
Save kateolenya/e8742158822f5b12e29d125783888ee8 to your computer and use it in GitHub Desktop.
Breaking basic encapsulation in Python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
class Phone: | |
username = "Kate" # public variable | |
__serial_number = "11.22.33" # private variable | |
__how_many_times_turned_on = 0 # private variable | |
def call(self): # public method | |
print( "Ring-ring!" ) | |
def __turn_on(self): # private method | |
self.__how_many_times_turned_on += 1 | |
print( "Times was turned on: ", self.__how_many_times_turned_on ) | |
my_phone = Phone() | |
my_phone._Phone__turn_on() | |
my_phone._Phone__serial_number = "44.55.66" | |
print( "New serial number is ", my_phone._Phone__serial_number ) | |
input( "Press Enter to exit" ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment