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: 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 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 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: 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 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