Skip to content

Instantly share code, notes, and snippets.

@lucasrcezimbra
Created February 18, 2021 19:12
Show Gist options
  • Save lucasrcezimbra/db9fcf1d47281e6dd2834e60efc12ab8 to your computer and use it in GitHub Desktop.
Save lucasrcezimbra/db9fcf1d47281e6dd2834e60efc12ab8 to your computer and use it in GitHub Desktop.
class cached_property(property):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.cache_name = f'_{self.fget.__name__}'
def __get__(self, obj, type):
if not hasattr(obj, self.cache_name):
value = super().__get__(obj, type)
setattr(obj, self.cache_name, value)
return getattr(obj, self.cache_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment