
On Tue, Aug 4, 2020 at 3:35 AM Christopher Barker <pythonchb@gmail.com> wrote:
On Sat, Aug 1, 2020 at 6:10 PM Inada Naoki <songofacandy@gmail.com> wrote:
Repacking is mutation, and mutating dict while iterating it breaks the iterator. But `d.items()[42]` don't looks like mutation.
Pardon my ignorance, but IS repacking a mutation? Clearly it's mutating the internal state, but at the logical structure of the dict will not have changed.
You are totally right. Repacking mutates internal state so it breaks iterator. But it doesn't look like mutation from users' point of view. It is the main problem. We can not repack silently.
Though I suppose if it's being iterated over, then the iterator is keeping an index into the internal array, which would change on repacking?
Yes.
which means that it's worse than not looking like a mutation, but it could make active iterators return results that are actually incorrect.
In most cases, iterator can detect it and raise RuntimeError.
I have to think that this could be accommodated somehow, but with ugly special case code, so yeah, not worth it. Though you could keep track of if there are any active views (ir even active iterators) and not repack in that case. I'm sure most dict iterators are most commonly used right away.
Is repacking ever currently done with dicts? If so, then how is this issue worked around?
Repacking happens only when insertion resize; when inserting an item but there is no space to insert. dict.clear() also creates clean empty dict. Currently, del/pop doesn't cause repacking. https://bugs.python.org/issue32623 Regards, -- Inada Naoki <songofacandy@gmail.com>