Skip to content

Instantly share code, notes, and snippets.

@raek
Created July 7, 2014 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raek/9ffde9e8dda812e0ce8c to your computer and use it in GitHub Desktop.
Save raek/9ffde9e8dda812e0ce8c to your computer and use it in GitHub Desktop.
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