Created
February 3, 2012 16:38
-
-
Save signed0/1731010 to your computer and use it in GitHub Desktop.
Lazy loding method decorator
This file contains 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
'''' | |
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