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

Guido van Rossum guido at python.org
Mon Nov 17 19:17:21 CET 2014


On Mon, Nov 17, 2014 at 10:09 AM, Chris Angelico <rosuav at gmail.com> wrote:

> On Tue, Nov 18, 2014 at 4:40 AM, Guido van Rossum <guido at python.org>
> wrote:
> > Wapping each result would just result in an extra allocation +
> deallocation
> > per iteration...
>
> Which is why I would be more inclined to use a sentinel of some
> sort... but that has its own problems. There's no perfect solution, so
> status quo wins unless a really compelling case can be made. I could
> toss something into the Alternate Proposals section, but I wouldn't be
> personally supporting it.
>

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).

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).

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141117/7d74b374/attachment.html>


More information about the Python-ideas mailing list