[issue44992] functools.lru_cache does not consider strings and numpy strings as equivalent

Raymond Hettinger report at bugs.python.org
Tue Aug 24 12:17:53 EDT 2021


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

Thanks for the report but this is an allowed behavior and not a bug.

Per the docs¹:  If typed is set to true, function arguments of different types will be cached separately. For example, f(3) and f(3.0) will always be treated as distinct calls with distinct results. If typed is false, the implementation will usually but not always regard them as equivalent calls and only cache a single result. 

¹ https://docs.python.org/3.10/library/functools.html#module-functools

In this particular case, exact type matches for str and int have an alternate path that tends to save space.

The cost is that str or int equivalents are cached separately.  That doesn't tend to be a problem in practice because functions are mostly called with the same types over and over again.

Note, we also treat equivalent calling patterns as distinct, f(a=1, b=2), is cached separately from f(b=2, a=1).

----------
nosy: +rhettinger
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

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


More information about the Python-bugs-list mailing list