Skip to content

Instantly share code, notes, and snippets.

@itkr
Created January 23, 2015 15:19
Show Gist options
  • Save itkr/4ca1d1070a6fa87ba6c7 to your computer and use it in GitHub Desktop.
Save itkr/4ca1d1070a6fa87ba6c7 to your computer and use it in GitHub Desktop.
cached_property
def cached_property(func):
@property
def f(self):
name = "_{}_cache".format(func.__name__)
if not hasattr(self, name):
setattr(self, name, func(self))
return getattr(self, name)
return f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment