You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Make lru_cache(maxsize=None) more discoverable #84751
The recent discussions on python-ideas showed that people have a hard time finding the infinity-cache option for lru_cache(). Also, in the context of straight caching without limits, the name *lru_cache()* makes the tool seem complex and heavy when in fact, it is simple, lightweight, and fast (doing no more than a simple dictionary lookup).
We could easily solve both problems with a helper function:
defcache(func):
'Simple unbounded cache. Sometimes called "memoize".'returnlru_cache(maxsize=None, typed=False)
There was some discussion about a completely new decorator with different semantics (holding a lock across a call to an arbitrary user function and being limited to zero argument functions). It all the examples that were presented, this @cache decorator would suffice. None of examples presented actually locking behavior.
FWIW, this doesn't preclude the other proposal if it ever gains traction and moves forward. This just takes existing functionality and improves clarity and discoverability.
The core issue is that if you only want a simple unbounded cache, it isn't obvious that that behavior is buried in the lru_cache() API. In hindsight, this always should have been separate functionality. And some day we may deprecate the maxsize=None option which is somewhat opaque.
I know it is really really late now. But we should really add this change to What’s New In Python 3.9 because it is actually an interesting and important new feature.
Activity
rhettinger commentedon May 8, 2020
The recent discussions on python-ideas showed that people have a hard time finding the infinity-cache option for lru_cache(). Also, in the context of straight caching without limits, the name *lru_cache()* makes the tool seem complex and heavy when in fact, it is simple, lightweight, and fast (doing no more than a simple dictionary lookup).
We could easily solve both problems with a helper function:
It would be used like this:
There was some discussion about a completely new decorator with different semantics (holding a lock across a call to an arbitrary user function and being limited to zero argument functions). It all the examples that were presented, this @cache decorator would suffice. None of examples presented actually locking behavior.
rhettinger commentedon May 8, 2020
FWIW, this doesn't preclude the other proposal if it ever gains traction and moves forward. This just takes existing functionality and improves clarity and discoverability.
The core issue is that if you only want a simple unbounded cache, it isn't obvious that that behavior is buried in the lru_cache() API. In hindsight, this always should have been separate functionality. And some day we may deprecate the maxsize=None option which is somewhat opaque.
rhettinger commentedon May 12, 2020
New changeset 21cdb71 by Raymond Hettinger in branch 'master':
bpo-40571: Make lru_cache(maxsize=None) more discoverable (GH-20019)
21cdb71
tasty0tomato commentedon Oct 25, 2024
I know it is really really late now. But we should really add this change to What’s New In Python 3.9 because it is actually an interesting and important new feature.