Skip to content

Instantly share code, notes, and snippets.

@Swalloow
Created April 22, 2018 08:59
Show Gist options
  • Save Swalloow/16513e81d0e59884e93d6b2dfe3b3429 to your computer and use it in GitHub Desktop.
Save Swalloow/16513e81d0e59884e93d6b2dfe3b3429 to your computer and use it in GitHub Desktop.
import functools
def lazy_property(function):
attribute = '_cache_' + function.__name__
@property
@functools.wraps(function)
def decorator(self):
if not hasattr(self, attribute):
setattr(self, attribute, function(self))
return getattr(self, attribute)
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment