Created
September 10, 2018 15:09
-
-
Save gotraveltoworld/588aa1dc90f2b3d76eb586c065508f85 to your computer and use it in GitHub Desktop.
Use the 'property' to build a simply class.
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
# Basic's getter and setter. | |
class Car(object): | |
__wheels = 4 | |
def get_wheels(self): | |
return self.__wheels | |
def set_wheels(self, value=4): | |
self.__wheels = value | |
car = Car() | |
car.set_wheels(6) | |
print(car.get_wheels()) # 6 | |
# Property decorator. | |
class Car_Property(object): | |
__wheels = 4 | |
@property | |
def get_wheels(self): | |
return self.__wheels | |
@get_wheels.setter | |
def set_wheels(self, value=4): | |
self.__wheels = value | |
car_property = Car_Property() | |
car_property.set_wheels = 8 | |
print(car_property.get_wheels) # 8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment