Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #3701
| X-FeedAbuse | http://nntpfeed.proxad.net/abuse.pl feeded by 88.191.16.109 |
|---|---|
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!nospam.fr.eu.org!usenet-fr.net!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| 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; 'def': 0.13; 'error:': 0.14; 'wrote:': 0.14; 'explanation': 0.16; '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; '...': 0.32; 'created': 0.33; 'using': 0.34; '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:40:12 +0200 |
| Organization | None |
| References | <4DAED72B.2030400@shopzeus.com> <iomnbl$7v1$1@dough.gmane.org> |
| 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 <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.645.1303306815.9059.python-list@python.org> (permalink) |
| Lines | 73 |
| NNTP-Posting-Host | 82.94.164.166 |
| X-Trace | 1303306815 news.xs4all.nl 81475 [::ffff:82.94.164.166]:34179 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:3701 |
Show key headers only | View raw
Peter Otten wrote:
> 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):
Hm, I see you are using has_key() which is 2.x-only and invalidates my
explanation :(
>> 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 "<stdin>", line 1, in <module>
> 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']
Back to comp.lang.python | Previous | Next | Find similar
Re: dictionary size changed during iteration Peter Otten <__peter__@web.de> - 2011-04-20 15:40 +0200
csiph-web