Skip to content

Instantly share code, notes, and snippets.

@raek
Created July 7, 2014 23:26
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/a2b62929980897db9a13 to your computer and use it in GitHub Desktop.
Save raek/a2b62929980897db9a13 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.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