Skip to content

Instantly share code, notes, and snippets.

@jesserobertson
Last active August 29, 2015 14:02
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 jesserobertson/cc3cf45a1fbdb6c6083b to your computer and use it in GitHub Desktop.
Save jesserobertson/cc3cf45a1fbdb6c6083b to your computer and use it in GitHub Desktop.
Demo of a property on a class
class Foo(object):
def __init__(self):
self.datasource = something
self.cache = {}
@property
def data(self):
if 'data' in cache.keys():
return cache['data']
else:
cache['data'] = get_data(self.datasource)
return cache['data']
@data.setter
def data(self, new_data):
# Update the cache
cache['data'] = new_data
f = Foo()
f.data # returns the data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment