[Python-Dev] PEP 479: Change StopIteration handling inside generators
Ron Adam
ron3200 at gmail.com
Mon Nov 24 00:18:37 CET 2014
On 11/23/2014 04:15 PM, Chris Angelico wrote:
> On Mon, Nov 24, 2014 at 5:28 AM, Ron Adam<ron3200 at gmail.com> wrote:
>> >With the passage of the PEP, it will change what is different about them
>> >once it's in full effect. The stop hack won't work in both, and you may get
>> >a RuntimeError in generator expressions where you would get StopIteration in
>> >list-comps. (Is this correct?)
> "them" being list comps and generator expressions?
Yes
> The stop hack won't work in either (currently it does work in
> genexps), but you'd get a different exception type if you attempt it.
> This is correct. It's broadly similar to this distinction:
>
>>>> >>>{1:2,3:4}[50]
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> KeyError: 50
>>>> >>>[1,2,3,4][50]
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> IndexError: list index out of range
Comprehensions insert/append items, so you wouldn't get those unless you
are reading from another object and would bubble out of generators too.
Which is good because it's most likely an error that needs fixing.
> In both lists and dicts, you can't look up something that isn't there.
> But you get a slightly different exception type (granted, these two do
> have a common superclass) depending on the exact context. But the
> behaviour will be effectively the same.
I think the difference is very specific to StopIteration.
And I think we need to wait and see what happens in real code.
Cheers,
Ron
More information about the Python-Dev
mailing list