Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kateolenya
Last active March 4, 2019 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kateolenya/e8742158822f5b12e29d125783888ee8 to your computer and use it in GitHub Desktop.
Save kateolenya/e8742158822f5b12e29d125783888ee8 to your computer and use it in GitHub Desktop.
Breaking basic encapsulation in Python 3
#!/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