[Python-Dev] Re: A cute new way to get an infinite loop

Beni Cherniavsky cben at users.sf.net
Thu Sep 23 19:24:35 CEST 2004


Tim Peters wrote:
>>>>x = [1]
>>>>x.extend(-y for y in x)
> 
A simpler way:

 >>> x = [1, -1]
 >>> x.extend(iter(x))

Curiously, this didn't "work" before 2.4 either:

 >>> x = [1]
 >>> x.extend(iter(x))
 >>> x
[1, 1]

The iterator did see the new elements after the extend call but not
during it:

 >>> x = [1]
 >>> i = iter(x)
 >>> x.extend(x)
 >>> list(i)
[1, 1]
 >>> x = [1]
 >>> i = iter(x)
 >>> x.extend([list(i)])
 >>> x
[1, [1]]

The reason is that in 2.3 `listextend()` passed the right argument
through `PySequence_Fast` which copied it before beggining to extend
the list.

It's much better now.  I mean it!  Bugs should be predictable.
Infinite loop should never terminate silently.  Unless explicitly
terminated.



More information about the Python-Dev mailing list