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.

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?

which means that it's worse than not looking like a mutation, but it could make active iterators return results that are actually incorrect.

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?


Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython