Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.