Last active
August 29, 2015 14:02
-
-
Save jesserobertson/cc3cf45a1fbdb6c6083b to your computer and use it in GitHub Desktop.
Demo of a property on a class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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