Created
December 21, 2014 09:11
-
-
Save asafge/f0a5661bf6d2db7e70c4 to your computer and use it in GitHub Desktop.
Python lazy property
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
class LazyProperty(object): | |
""" | |
A descriptor protocol implementation used for wrapping a lambda | |
function (without parameters) to behave like property access. | |
Usage, only within a class definition: all_users = LazyProperty(lambda: []) | |
""" | |
@staticmethod | |
def is_lambda(v): | |
lambda_example = lambda: 0 | |
return isinstance(v, type(lambda_example)) and v.__name__ == lambda_example.__name__ | |
@staticmethod | |
def has_params(v): | |
return len(inspect.getargspec(v).args) > 0 | |
def __init__(self, *args, **kwargs): | |
for value in args: | |
if LazyProperty.is_lambda(value): | |
if LazyProperty.has_params(value): | |
raise Exception('Lambda function parameters are not allowed.') | |
self.func = value | |
return | |
if not getattr(self, 'func', None): | |
raise Exception('A LazyProperty instance should be initialized with a lambda function.') | |
def __get__(self, *args, **kwargs): | |
if not getattr(self, 'res', None): | |
self.res = self.func() | |
return self.res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment