I have one variable whose content I need to get from a remote server, so I would rather wait until its content is really needed. I thought of doing if using a property but it seems I’m doing something wrong. Follow the example.
def download():
return 'content from remote server'
class Foo:
def __init__(self):
self.downloaded_bar = False
self.bar = None
@property
def bar():
if not self.downloaded:
self.bar = download()
self.downloaded = True
return self.bar
f = Foo()
print f.bar #prints None, I expected 'content from remote server'
What am I doing wrong?
You need to make
Fooa new style class for descriptors to work.You also can’t have a property with the same name as the attribute, so I changed
.barto._barYou’ll also need to fix the typo (downloaded_bar)
Actually you probably don’t need the
downloadedattribute at all