-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Iterating over ChainMap.values() and ChainMap.items() updates underlying defaultdicts #93432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
(Sorry for editing the previous comment: it will break other behavior.)
This behavior looks like it is intended because this is a matter of look-up order. Footnotes |
Yes, it looks like an intentional behavior. There is even a comment in the code about this: cpython/Lib/collections/__init__.py Line 1001 in 8150b8c
|
To the extent that this is a problem, it lies with the defaultdict updating as a side-effect of lookups. If that isn't desired, it isn't hard to define a dict subclass with |
FWIW, you can turn-off defaultdict's updating behavior prior to iteration:
|
Bug report
Hello,
I encountered some confusing behavior when combining ChainMap with defaultdicts. Might be related to #23534
Specifically, iterating over the values or items of the ChainMap updates one of the underlying dictionaries, and returns the wrong result.
Just iterating over they keys or the dictionary directly produces the right results still and doesn't update the underlying dictionaries.
Your environment
Tested on Python version 3.10.4.
Linux 5.13.0-41-generic #46~20.04.1-Ubuntu SMP
x86_64 x86_64 x86_64 GNU/Linux
The text was updated successfully, but these errors were encountered: