Created
July 7, 2014 23:34
-
-
Save raek/9ffde9e8dda812e0ce8c to your computer and use it in GitHub Desktop.
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
class Foo(object): | |
def __init__(self, a_value): | |
self._a = None # Create private instance variable: _a | |
self.a = a_value # Re-use the setter method (in the form of a property) to do validation and set the actual value | |
@property | |
def a(self): # used to be called "get_a" | |
return self._a | |
@a.setter | |
def a(self, new_value): # used to be called "set_a" | |
if new_value < 0: | |
raise ValueError("a must be non-negative") | |
else: | |
self._a = new_value | |
f = Foo(123) | |
print(f.a) | |
f.a = 1 | |
f.a = -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment