Skip to content

Instantly share code, notes, and snippets.

@borntyping
Created May 17, 2013 10:53
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 borntyping/5598388 to your computer and use it in GitHub Desktop.
Save borntyping/5598388 to your computer and use it in GitHub Desktop.
class Attribute(object):
def __get__(self, instance, owner):
print("Getting x")
if not 'x' in instance.__dict__:
instance.__dict__['x'] = 1
return instance.__dict__['x']
def __set__(self, instance, value):
instance.__dict__['x'] = value
class Object(object):
x = Attribute()
o = Object()
print(o.x)
# Getting x
# 1
o.x = 2
print(o.x)
# Getting x
# 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment