[issue35300] Bug with memoization and mutable objects
Change by bolorsociedad : -- components: Library (Lib) nosy: bolorsociedad priority: normal severity: normal status: open title: Bug with memoization and mutable objects type: behavior versions: Python 3.4, Python 3.5, Python 3.6, Python 3.7, Python 3.8 ___ Python tracker <https://bugs.python.org/issue35300> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue35300] Bug with memoization and mutable objects
New submission from bolorsociedad : 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 <https://bugs.python.org/issue35300> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue35300] Bug with memoization and mutable objects
bolorsociedad added the comment: I understand it may be inefficient sometimes. Perhaps it would be nice to add an argument to lru_cache to specify that we want to deep copy? Something like def lru_cache(..., deepcopy=False): ... -- ___ Python tracker <https://bugs.python.org/issue35300> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com