Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41108 > unrolled thread
| Started by | Nick Mellor <thebalancepro@gmail.com> |
|---|---|
| First post | 2013-03-11 22:35 -0700 |
| Last post | 2013-03-13 05:02 +0100 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
del not working for (exhausted) dict iterable value (Python 3.3) Nick Mellor <thebalancepro@gmail.com> - 2013-03-11 22:35 -0700
Re: del not working for (exhausted) dict iterable value (Python 3.3) alex23 <wuwei23@gmail.com> - 2013-03-11 22:52 -0700
Re: del not working for (exhausted) dict iterable value (Python 3.3) Nick Mellor <thebalancepro@gmail.com> - 2013-03-12 15:40 -0700
Re: del not working for (exhausted) dict iterable value (Python 3.3) Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2013-03-13 05:02 +0100
| From | Nick Mellor <thebalancepro@gmail.com> |
|---|---|
| Date | 2013-03-11 22:35 -0700 |
| Subject | del not working for (exhausted) dict iterable value (Python 3.3) |
| Message-ID | <2879f537-1666-46f7-abbf-38204d8184c2@googlegroups.com> |
Hi all,
event['Items'] is an exhausted (all used up) iterable.
Now I do the following (lines 142-4):
event.update({'Attributes': filtered_attributes})
del event['Items']
yield event
and get a KeyError on the del statement. 'Items' is a key in the event dictionary at this point.
The full file is here:
https://gist.github.com/nickmellor/5140516
I can get round it by copying all but the 'Items' member to a new dictionary, but obviously I don't want to.
Is there some deep design in Python here, that it won't delete a dict value that's an (exhausted) iterator, or have I found a bug?
Thanks for any help,
Nick
[toc] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2013-03-11 22:52 -0700 |
| Message-ID | <0809a262-494f-46d6-910a-909a06e2aa92@m9g2000pby.googlegroups.com> |
| In reply to | #41108 |
On Mar 12, 3:35 pm, Nick Mellor <thebalance...@gmail.com> wrote:
> event['Items'] is an exhausted (all used up) iterable.
>
> Now I do the following (lines 142-4):
>
> event.update({'Attributes': filtered_attributes})
> del event['Items']
> yield event
>
> and get a KeyError on the del statement.
>
> Is there some deep design in Python here, that it won't delete a
> dict value that's an (exhausted) iterator, or have I found a bug?
You're effectively doing this:
>>> event = dict(Items=[1,2,3])
>>> for e in event['Items']:
... del event['Items']
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
KeyError: 'Items'
You want to move your del statement up an indentation level so it
happens after the iterator is actually exhausted, and not after the
first iteration.
[toc] | [prev] | [next] | [standalone]
| From | Nick Mellor <thebalancepro@gmail.com> |
|---|---|
| Date | 2013-03-12 15:40 -0700 |
| Message-ID | <9781042e-786c-4e5f-8a2e-b77d6fabc593@googlegroups.com> |
| In reply to | #41109 |
Thanks Alex! Nick
[toc] | [prev] | [next] | [standalone]
| From | Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> |
|---|---|
| Date | 2013-03-13 05:02 +0100 |
| Message-ID | <khotp3$sja$1@r03.glglgl.gl> |
| In reply to | #41109 |
Am 12.03.2013 06:52 schrieb alex23:
> You're effectively doing this:
>
>>>> event = dict(Items=[1,2,3])
>>>> for e in event['Items']:
> ... del event['Items']
> ...
> Traceback (most recent call last):
> File "<stdin>", line 2, in <module>
> KeyError: 'Items'
>
> You want to move your del statement up an indentation level so it
> happens after the iterator is actually exhausted, and not after the
> first iteration.
Just to be clear: Exhausting the iterator is not the problem, as I
thought as well at the first glance.
The problem is the fact that the loop body tuns multiple times - and so
does the del statement. A
event = dict(Items=[1,2,3])
for e in event['Items']:
if 'Items' in event: del event['Items']
runs perfectly, as the iterable is transformed to an iterator at the
very start of the loop.
Thomas
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web