[New-bugs-announce] [issue42487] collections.ChainMap.__iter__ calls __getitem__ on underlying maps

Andreas Poehlmann report at bugs.python.org
Fri Nov 27 19:50:30 EST 2020


New submission from Andreas Poehlmann <andreas at poehlmann.io>:

Hello,

I encountered an issue with collections.ChainMap, that was introduced when https://bugs.python.org/issue32792 got fixed.

Iterating a ChainMap will call __getitem__ on its underlying maps:


>>> from collections import UserDict, ChainMap
>>> class MyDict(UserDict):
...     def __getitem__(self, k):
...         print("expensive computation", k)
...         return super().__getitem__(k)
... 
>>> set(ChainMap(MyDict(a=1, b=2, c=3)))
expensive computation a
expensive computation b
expensive computation c
{'c', 'b', 'a'}

----------
components: Library (Lib)
messages: 381971
nosy: ap--
priority: normal
severity: normal
status: open
title: collections.ChainMap.__iter__ calls __getitem__ on underlying maps
type: behavior
versions: Python 3.10, Python 3.7, Python 3.8, Python 3.9

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


More information about the New-bugs-announce mailing list