diff options
-rw-r--r-- | ranger/ext/lazy_property.py | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/ranger/ext/lazy_property.py b/ranger/ext/lazy_property.py index 92dc309d..c468a8c9 100644 --- a/ranger/ext/lazy_property.py +++ b/ranger/ext/lazy_property.py @@ -1,4 +1,6 @@ -# From http://blog.pythonisito.com/2008/08/lazy-descriptors.html +# This file is part of ranger, the console file manager. +# License: GNU GPL version 3, see the file "AUTHORS" for details. +# Based on http://blog.pythonisito.com/2008/08/lazy-descriptors.html from __future__ import (absolute_import, division, print_function) @@ -17,6 +19,22 @@ class lazy_property(object): # pylint: disable=invalid-name,too-few-public-meth 42 >>> foo.answer 42 + >>> foo.answer__reset() + >>> foo.answer + calculating answer... + 42 + >>> foo.answer + 42 + + Avoid interaction between multiple objects: + + >>> bar = Foo() + >>> bar.answer + calculating answer... + 42 + >>> foo.answer__reset() + >>> bar.answer + 42 """ def __init__(self, method): @@ -27,8 +45,14 @@ class lazy_property(object): # pylint: disable=invalid-name,too-few-public-meth def __get__(self, obj, cls=None): if obj is None: # to fix issues with pydoc return None + + def reset_function(): + setattr(obj, self.__name__, self) + del obj.__dict__[self.__name__] # force "__get__" being called + result = self._method(obj) obj.__dict__[self.__name__] = result + obj.__dict__[self.__name__ + "__reset"] = reset_function return result |