[Python-Dev] PEP 479: Change StopIteration handling inside generators

Chris Angelico rosuav at gmail.com
Sun Nov 23 23:15:10 CET 2014


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?

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

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.

ChrisA


More information about the Python-Dev mailing list