Created
July 7, 2014 23:26
-
-
Save raek/a2b62929980897db9a13 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.set_a(a_value) # Re-use the setter method so do validation and set the actual value | |
def get_a(self): | |
return self._a | |
def set_a(self, new_value): | |
if new_value < 0: | |
raise ValueError("a must be non-negative") | |
else: | |
self._a = new_value | |
f = Foo(123) | |
print(f.get_a()) | |
f.set_a(1) | |
f.set_a(-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment