[issue21145] Add the @cached_property decorator

Raymond Hettinger report at bugs.python.org
Tue May 15 19:55:40 EDT 2018


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

FWIW, over the past decade, I've used variants of CachedProperty a number of times and have often had issues that later required messing with its internals (needing a way to invalidate or clear the cache, mock patching the underlying function for testing, consistency between multiple cached properties cached in different threads, inability to run the method through a debugger, inadvertently killing logging or other instrumentation, moving the cache valued from an instance variables to an external weakref dictionary etc).

I proposed the idea of a CachedProperty in descriptor tutorials over a decade ago.  Since then, I've grown wary of the idea of making them available for general use.  Instead, we're better with a recipe that someone can use to build their understanding and then customize as necessary.  The basic recipe is simple so there isn't much of a value add by putting this in the standard library.

If we want to add another property() variant, the one I've had the best luck with is CommonProperty() which lets you re-use the same getter and setter methods for multiple properties (the name of the property variable gets passed in as the first argument).

----------
versions: +Python 3.8 -Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue21145>
_______________________________________


More information about the Python-bugs-list mailing list