[Python-Dev] More data points

Tim Peters tim.peters at gmail.com
Sun Sep 26 07:50:50 CEST 2004

[ncoghlan at iinet.net.au]
> Returning to Tim's original infinite loop, the behaviour is interestingly variable.
> List and array go into the infinite loop.

What happens when you mutate a list while iterating over it is
defined, and an infinite loop is expected for that.  Ditto for array.

> Deque and dictionary both detect that the loop variable has been mutated and
> throw a specific exception.

That's because they never suffered from list's ill-advised
documentation effectively blessing mutation while iterating <0.5

> Set throws the same exception as dictionary does (presumably, the main
> container inside 'set' is a dictionary)
> Details of behaviour:

The last one is extremely surprising:

> Python 2.4a3 (#16, Sep 21 2004, 17:33:57)
> [GCC 3.4.1 20040702 (Red Hat Linux 3.4.1-2)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.


> >>> x
> {1: None, -1: None}
> >>> x.fromkeys(-y for y in x)
> {-1: None}

Are you sure get that?  I get this:

>>> x
{1: None, -1: None}
>>> x.fromkeys(-y for y in x)
{1: None, -1: None}

"x.fromkeys()" doesn't have anything to do with x.  Any dict works same there:

>>> {}.fromkeys(-y for y in x)
{1: None, -1: None}
>>> {'a': 'b', 'c': 'd', 'e': 'f'}.fromkeys(-y for y in x)
{1: None, -1: None}

More information about the Python-Dev mailing list