Skip to content

Instantly share code, notes, and snippets.

@kevinjqiu
Created February 19, 2014 19:21
Show Gist options
  • Save kevinjqiu/9099545 to your computer and use it in GitHub Desktop.
Save kevinjqiu/9099545 to your computer and use it in GitHub Desktop.
class AgeAttr(object):
def __get__(self, instance, owner):
return instance.value
def __set__(self, instance, value):
if value < 0:
raise ValueError('Age cannot be negative')
instance.value = value
class User(object):
age = AgeAttr()
class Account(object):
age = AgeAttr()
u = User()
u.age = 1
print u.age
a = Account()
a.age = -4 # ValueError: Age cannot be negative
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment