[issue35300] Bug with memoization and mutable objects

bolorsociedad report at bugs.python.org
Fri Nov 23 06:53:18 EST 2018


New submission from bolorsociedad <perezarribas.imanol at gmail.com>:

The decorator functools.lru_cache seems to not work properly when the function to be memoized returns a mutable object.

For instance:

>>> import functools
>>> @functools.lru_cache()
... def f(x):
...    return [x, x + 1]
... 
>>> a = f(4)
>>> print(a)
[4, 5]
>>> a[0] = 0
>>> f(4)
[0, 5]


When the returned mutable object is modified, the cache is modified as well. In my opinion, functools.lru_cache should store a deep copy of the returned object.

----------

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


More information about the Python-bugs-list mailing list