Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def cached_property(method):
- method.cache = {}
- def caching_method(self, *args, **kwargs):
- key = '%s__%s' % (hex(id(self)), method.__name__)
- try:
- cache = method.cache
- except AttributeError:
- cache = method.cache = {}
- try:
- value = cache[key]
- except KeyError:
- value = method(self, *args, **kwargs)
- cache[key] = value
- return value
- caching_method.__doc__ = method.__doc__
- caching_method.__name__ = method.__name__
- caching_method.__module__ = method.__module__
- return property(caching_method)
- class SuperSafe(object):
- @cached_property
- def value(self):
- """Some docstring."""
- return random.randint(1, 10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement