[Python-Dev] More data points

ncoghlan at iinet.net.au ncoghlan at iinet.net.au
Sun Sep 26 07:28:28 CEST 2004


Quoting Raymond Hettinger <python at rcn.com>:
> To help with get a comprehensive view when I look at this more closely
> tomorrow, can you try out variations on the theme with other mutables:
> 
>   myset.update 
>   deque.extend
>   dict.update
>   dict.fromkeys
>   array.extend

Returning to Tim's original infinite loop, the behaviour is interestingly variable.

List and array go into the infinite loop. Deque and dictionary both detect that
the loop variable has been mutated and throw a specific exception. Set throws
the same exception as dictionary does (presumably, the main container inside
'set' is a dictionary)

Details of behaviour:

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>> x = [1]
X>> x.extend(-y for y in x)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 1, in <generator expression>
KeyboardInterrupt
X>> len(x)
73727215
X>> x = set([1])
X>> x
set([1])
X>> x.update(-y for y in x)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 1, in <generator expression>
RuntimeError: dictionary changed size during iteration
X>> x
set([1, -1])
X>> from collections import deque
X>> x = deque([1])
X>> x.extend(-y for y in x)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 1, in <generator expression>
RuntimeError: deque changed size during iteration
X>> x
deque([1, -1])
X>> from array import array
X>> x = array('b', '1')
X>> x.extend(-y for y in x)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 1, in <generator expression>
KeyboardInterrupt
X>> len(x)
6327343
X>> x = dict.fromkeys([1])
X>> x
{1: None}
X>> x.update((-y, None) for y in x)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 1, in <generator expression>
RuntimeError: dictionary changed size during iteration
X>> x
{1: None, -1: None}
X>> x.fromkeys(-y for y in x)
{-1: None}




More information about the Python-Dev mailing list