Issue 33774: Document that @lru_cache caches based on exactly how the function arguments are specified (original) (raw)

Created on 2018-06-05 16:58 by solstag, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 9298 merged rhettinger,2018-09-14 07:37
PR 9299 merged miss-islington,2018-09-14 07:55
Messages (7)
msg318770 - (view) Author: Al-Scandar Solstag (solstag) Date: 2018-06-05 16:58
Ni! It is not clear at all in the documentation of @lru_cache that the cache takes into account the exact way the function was called, not the values passed to its arguments, as one could/would expect. I mean that for function(a, b, c=3) the three calls below are not considered equivalent as far as the cache is concerned: function(1, 2, 3) function(1, 2, c=3) function(1, 2) I hope this can be clarified in the documentation. I wasted a great deal of time today trying to understand why my calls were not getting cached and only figured it out when I decided to go read @lru_cache's code. It seems very likely that other people have had the same problem. Or worse, people might be using @lru_cache believing it is working when it isn't. Cheers!
msg318774 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-06-05 17:25
Agreed that this should be documented.
msg318793 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-06-06 01:07
Sure, I can add a line mentioning that distinct argument patterns may be considered as distinct cache entries even though they otherwise seem to be equivalent calls. That will just be a general statement though. The specific details are implementation dependent, have changed over time, and may change again in the future.
msg318822 - (view) Author: Al-Scandar Solstag (solstag) Date: 2018-06-06 13:48
Hi Raymond, I think I understand what you mean, and would suggest something along the lines of: """ Note that lru_cache only guarantees cache matches on the exact way function arguments are specified, so the following ways of calling 'def f(a, b=7)' are not guaranteed to cache each other: f(1), f(a=1), f(1, 7), f(1, b=7), f(a=1, b=7). """
msg320358 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-06-24 06:24
I propose this, "Distinct argument patterns may be considered to be distinct calls with distinct results even if the underlying function sees them as equivalent calls."
msg320422 - (view) Author: Al-Scandar Solstag (solstag) Date: 2018-06-25 13:28
Speaking frankly, I might not have grasped what might happen by reading your line. I think having at least a minimal example is crucial. Perhaps: "Distinct argument patterns, such as `f(1)` and `f(first_arg=1)`, may not cache for each other even if the underlying function sees them as equivalent calls."
msg320430 - (view) Author: Srinivas Reddy Thatiparthy(శ్రీనివాస్ రెడ్డి తాటిపర్తి) (thatiparthy) * Date: 2018-06-25 17:06
Hi Raymond, I find your statement hard to understand.I agree with Solstag, it is always helpful to have an example. +1 for solstag wording.
History
Date User Action Args
2022-04-11 14:59:01 admin set github: 77955
2018-09-14 08:14:28 rhettinger set status: open -> closedresolution: fixedstage: patch review -> resolved
2018-09-14 07:55:12 miss-islington set pull_requests: + <pull%5Frequest8728>
2018-09-14 07:37:36 rhettinger set keywords: + patchstage: needs patch -> patch reviewpull_requests: + <pull%5Frequest8727>
2018-06-25 17:06:31 thatiparthy set nosy: + thatiparthymessages: +
2018-06-25 13:28:07 solstag set messages: +
2018-06-24 06:24:57 rhettinger set messages: +
2018-06-06 13:48:39 solstag set messages: +
2018-06-06 01:07:13 rhettinger set priority: normal -> lowassignee: docs@python -> rhettingermessages: +
2018-06-05 17:25:16 r.david.murray set nosy: + rhettinger, r.david.murraytitle: Improve doc of @lru_cache to avoid misuse and confusion -> Document that @lru_cache caches based on exactly how the function arguments are specifiedmessages: + versions: + Python 3.6, Python 3.7, Python 3.8stage: needs patch
2018-06-05 16:58:55 solstag create