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

Chris Angelico rosuav at gmail.com
Mon Nov 17 19:23:25 CET 2014


On Tue, Nov 18, 2014 at 5:17 AM, Guido van Rossum <guido at python.org> wrote:
> Trust me, we went down this rabbit hole when we designed the iterator
> protocol. The problem is that that sentinel object must have a name
> (otherwise how would you know when you had seen the sentinel), which means
> that there is at least one dict (the namespace defining that name, probably
> the builtins module) that has the sentinel as one of its values, which means
> that iterating over that particular dict's values would see the sentinel as
> a legitimate value (and terminate prematurely). You could fix this by
> allocating a unique sentinel for every iteration, but there would be some
> additional overhead for that too (the caller and the iterator both need to
> hold on to the sentinel object).

That's a much more solid argument against it than I had. (Also amusing
to try to contemplate.) The iterator protocol absolutely demands a
completely out-of-band means of signalling "I have nothing to return
now".

> In any case, as I tried to say before, redesigning the iterator protocol is
> most definitely out of scope here (you could write a separate PEP and I'd
> reject it instantly).

That's a PEP I'll leave for someone else to write. :)

ChrisA


More information about the Python-ideas mailing list