Skip to content

Instantly share code, notes, and snippets.

View kateolenya's full-sized avatar

Kateryna kateolenya

View GitHub Profile
@kateolenya
kateolenya / python_magic_enc.py
Last active March 5, 2019 12:51
Encapsulation with magic methods in Python 3
#!/usr/bin/python3
class Phone:
def __init__(self, number): # magic method / inititalizer
print( "The Phone object was created" )
self.number = number
def __lt__(self, other): # magic method / rich comparison
return self.number < other.number
@kateolenya
kateolenya / python_break_basic_enc.py
Last active March 4, 2019 13:31
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!" )
@kateolenya
kateolenya / python_basic_enc.py
Last active March 5, 2019 10:40
Basic encapsulation in Python3
#!/usr/bin/python3
class Phone:
username = "Kate" # public variable
__how_many_times_turned_on = 0 # private variable
def call(self): # public method
print( "Ring-ring!" )
def __turn_on(self): # private method
@kateolenya
kateolenya / python_basic_class.py
Last active March 4, 2019 13:30
Basic class in Python3
#!/usr/bin/python3
class Phone:
number = "111-11-11"
def print_number(self):
print( "Phone number is: ", self.number )
my_phone = Phone()
my_phone.print_number()
@kateolenya
kateolenya / python_enc.py
Last active March 4, 2019 13:30
Testing encapsulation in Python3
#!/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 __init__(self): # constructor
print( "The Phone object was created" )
self.__turn_on()