Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '2.x': 0.05; 'dictionary': 0.07; '3.x': 0.09; '>>>>': 0.09; 'exception:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; '>>>': 0.12; 'def': 0.13; 'error:': 0.14; 'wrote:': 0.14; 'iteration.': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'traceback': 0.16; '(most': 0.16; 'yield': 0.19; 'variable': 0.21; 'keys': 0.23; 'last):': 0.23; 'equivalent': 0.26; 'object': 0.27; 'changed': 0.27; 'error': 0.29; 'class': 0.29; 'explicitly': 0.29; 'list': 0.30; 'from:addr:web.de': 0.31; 'to:addr:python-list': 0.32; 'created': 0.33; 'header:X-Complaints-To:1': 0.34; 'file': 0.35; '"",': 0.35; 'error.': 0.36; 'getting': 0.36; 'less': 0.38; 'but': 0.38; 'used': 0.38; 'received:org': 0.38; 'to:addr:python.org': 0.39; 'header:Mime-Version:1': 0.39; 'how': 0.39; 'header:Received:5': 0.40; 'view': 0.64; 'here': 0.65; 'become': 0.70; 'message?': 0.84; 'subject:during': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: dictionary size changed during iteration Date: Wed, 20 Apr 2011 15:33:52 +0200 Organization: None References: <4DAED72B.2030400@shopzeus.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084d8c6.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 67 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303306435 news.xs4all.nl 81475 [::ffff:82.94.164.166]:58306 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3700 Laszlo Nagy wrote: > 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? The keys() method which used to return a list in 2.x was changed in 3.x to return a view object and to become more or less the equivalent of the old dict.iterkeys(): >>> d = dict(a=1) >>> keys = d.keys() >>> keys dict_keys(['a']) >>> for k in keys: ... d["b"] = 42 ... Traceback (most recent call last): File "", line 1, in RuntimeError: dictionary changed size during iteration >>> keys dict_keys(['a', 'b']) You now have to create the list explicitly to avoid the error: >>> d = dict(a=1) >>> keys = list(d.keys()) >>> for k in keys: ... d["b"] = 42 ... >>> d {'a': 1, 'b': 42} >>> keys ['a']