Python iterator when underlying object changes

前端 未结 3 1366
-上瘾入骨i
-上瘾入骨i 2021-01-20 11:37

I would like to know what\'s the general behaviour of an iterator if the underlaying object changes during the iteration.

Using a simple mutable list, it seems obvi

相关标签:
3条回答
  • 2021-01-20 11:53

    There's a comment on that particular issue in PEP 234 (Iterators):

    Once a particular iterator object has raised StopIteration, will it also raise StopIteration on all subsequent next() calls?

    Some say that it would be useful to require this, others say that it is useful to leave this open to individual iterators. Note that this may require an additional state bit for some iterator implementations (e.g. function-wrapping iterators).

    Resolution: once StopIteration is raised, calling it.next() continues to raise StopIteration.

    Note: this was in fact not implemented in Python 2.2; there are many cases where an iterator's next() method can raise StopIteration on one call but not on the next. This has been remedied in Python 2.3.

    0 讨论(0)
  • 2021-01-20 12:02

    There are three typical behaviours of changing an object while iterating over it:

    • the new data will be returned
    • the new data will be ignored
    • old data will be skipped

    In other words: the actual behaviour is undefined.

    Changing objects while iterating over them was such a common problem that in Python 3 the types set and dict (and possibly others) were changed to immediately raise an error if additions or removals were detected during iteration.

    0 讨论(0)
  • 2021-01-20 12:10

    There are some iterators that can yield more data even after a StopIteration has been raised; such iterators are considered broken.

    This does not mean the iterator is at fault -- it means that if you use such an iterator without caution you can end up with bugs and broken code.

    0 讨论(0)
提交回复
热议问题