Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #3695

dictionary size changed during iteration

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <gandalf@shopzeus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.038
X-Spam-Evidence '*H*': 0.92; '*S*': 0.00; 'dictionary': 0.07; 'exception:': 0.09; '>>>': 0.12; 'def': 0.13; 'iteration.': 0.16; 'thanks,': 0.17; 'yield': 0.19; 'cc:2**0': 0.20; 'variable': 0.21; 'changed': 0.27; 'error': 0.29; 'class': 0.29; 'to:addr:python- list': 0.32; 'created': 0.33; 'received:192.168.1': 0.34; 'received:192': 0.34; 'header:User-Agent:1': 0.35; 'error.': 0.36; 'getting': 0.36; 'received:192.168': 0.37; 'but': 0.38; 'to:addr:python.org': 0.39; 'how': 0.39; 'here': 0.65; 'message?': 0.84; 'subject:during': 0.84
Date Wed, 20 Apr 2011 14:52:59 +0200
From Laszlo Nagy <gandalf@shopzeus.com>
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.14) Gecko/20110223 Thunderbird/3.1.8
MIME-Version 1.0
To python-list@python.org
Subject dictionary size changed during iteration
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Cc Daniel Fekete <dani@shopzeus.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.640.1303304240.9059.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 82.94.164.166
X-Trace 1303304241 news.xs4all.nl 41114 [::ffff:82.94.164.166]:48049
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:3695

Show key headers only | View raw


Given this iterator:

class SomeIterableObject(object):
     ....
     ....

     def __iter__(self):
         ukeys = self.updates.keys()
         for key in ukeys:
             if self.updates.has_key(key):
                 yield self.updates[key]
         for rec in self.inserts:
             yield rec
     ....
     ....

How can I get this exception:

RuntimeError: dictionary changed size during iteration


It is true that self.updates is being changed during the iteration. But 
I have created the "ukeys" variable solely to prevent this kind of 
error. Here is a proof of correctness:

>>>  d = {1:1,2:2}
>>>  k = d.keys()
>>>  del d[1]
>>>  k
[1, 2]
>>>  k is d.keys()
False

So what is wrong with this iterator? Why am I getting this error message?

Thanks,

    Laszlo

Back to comp.lang.python | Previous | NextNext in thread | Find similar


Thread

dictionary size changed during iteration Laszlo Nagy <gandalf@shopzeus.com> - 2011-04-20 14:52 +0200
  Re: dictionary size changed during iteration Mel <mwilson@the-wire.com> - 2011-04-20 10:18 -0400
    Re: dictionary size changed during iteration Mel <mwilson@the-wire.com> - 2011-04-20 10:22 -0400
  Re: dictionary size changed during iteration John Nagle <nagle@animats.com> - 2011-04-21 23:22 -0700

csiph-web