Repository navigation
Option to make the lru_cache type specific #57436
Description
Activity
Based on a comment from Andrew Koenig, it is suggested that the lru_cache() offer an option to become type specific so that equal objects of different typed don't get cached to the same entry.
Here's an example:
@lru_cache(typed=True) def square(x): print('squaring', x) return x * x for x in [3, 3.0, 3, 3.0]: print(square(x))
Without type specificity, all four calls will return 9 rather than 9. 9.0, 9, 9.0.
First-pass patch attached.
- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
on Oct 19, 2011 Updated patch to include tests and an application to the re module.
The patch doesn't update the doc. You may also test type(square(3)). Except of these nits, the patch looks good.
The typed option is an important feature, and your patch contains a good example of its usage (the re module).
Features looks good, but the current patch doesn't handle keyword arguments correctly (more details in the Reitveld review).
Nick, Ezio, and Victor, thanks for looking at the code.
Here's an updated the patch:- optimization for fast globals() are now keyword-only arguments
- now types the keyword values, not the key/values tuples
- expanded tests
- added the doc update
Looks good to me (although it took me longer than it should have to figure out why you didn't need to store the keyword argument names a second time)
New changeset cba503a2c468 by Raymond Hettinger in branch 'default':
bpo-13227: Option to make the lru_cache() type specific (suggested by Andrew Koenig).
http://hg.python.org/cpython/rev/cba503a2c468Victor, Ezio, and Nick, thanks for the review.
Andrew, thanks for the feature request.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: