Skip to content

Instantly share code, notes, and snippets.

@rafales
Created June 13, 2013 14:37
Show Gist options
  • Save rafales/5774194 to your computer and use it in GitHub Desktop.
Save rafales/5774194 to your computer and use it in GitHub Desktop.
Python recipies
class cached_property(object):
"""
Decorator that converts a method with a single self argument into a
property cached on the instance.
"""
def __init__(self, func):
self.func = func
def __get__(self, instance, type=None):
if instance is None:
return self
res = instance.__dict__[self.func.__name__] = self.func(instance)
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment