Author steven.daprano
Recipients georg.brandl, steven.daprano
Date 2009-05-14.05:42:57
SpamBayes Score 5.53393e-10
Marked as misclassified No
Message-id <1242279843.94.0.529178267811.issue6017@psf.upfronthosting.co.za>
In-reply-to
Content
I'm not sure if this is a documentation bug or a behaviour bug, or 
possibly both.

The documentation warns about adding or deleting items from a dict 
while iterating over it:

"Using iteritems() while adding or deleting entries in the dictionary 
will raise a RuntimeError."

http://docs.python.org/library/stdtypes.html#dict.iteritems

Same for other dict iterators.

However, you can add and delete items, so long as the overall size of 
the dict doesn't change. Consequently, some modifications to the dict 
aren't caught, leading to various misbehaviour in (at least) Python 
2.5 and 2.6.

Some dicts appear to "run too long":

>>> d = dict(x=3, y=4)  # Two items
>>> it = d.iteritems()
>>> it.next()  # One
('y', 4)
>>> del d['y']
>>> d['z'] = 5
>>> it.next()  # Two
('x', 3)
>>> it.next()  # Three
('z', 5)


While others run too short:

>>> d = {-1: 'aa', -2: 'bb'}  # Two items
>>> it = d.iteritems()
>>> it.next()  # One
(-2, 'bb')
>>> del d[-1]
>>> d[0] = 'cc'
>>> it.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
History
Date User Action Args
2009-05-14 05:44:07steven.dapranosetrecipients: + steven.daprano, georg.brandl
2009-05-14 05:44:04steven.dapranosetmessageid: <1242279843.94.0.529178267811.issue6017@psf.upfronthosting.co.za>
2009-05-14 05:43:56steven.dapranolinkissue6017 messages
2009-05-14 05:43:46steven.dapranocreate