Skip to content

Instantly share code, notes, and snippets.

@signed0
Created February 3, 2012 16:38
Show Gist options
  • Save signed0/1731010 to your computer and use it in GitHub Desktop.
Save signed0/1731010 to your computer and use it in GitHub Desktop.
Lazy loding method decorator
''''
From http://code.activestate.com/recipes/363602-lazy-property-evaluation/
Usage:
class MyClass():
@lazyloaded
def config(self):
return {'yay': 'nay'}
foo = MyClass()
print foo.config['yay']
''''
class lazyloaded(object):
'''Allows one to wrap a method call so that the first call lazy loads the data
This only works for class methods
'''
def __init__(self, func):
self._func = func
def __get__(self, obj, _=None):
if obj is None:
return self
value = self._func(obj)
setattr(obj, self._func.func_name, value)
return value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment