<div dir="ltr">+1</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 10, 2014 at 8:06 AM, Oscar Benjamin <span dir="ltr"><<a href="mailto:oscar.j.benjamin@gmail.com" target="_blank">oscar.j.benjamin@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Somehow during the discussion of PEP 479 it was concluded that<br>
generators will become distinct from iterators. A few people have<br>
pointed out that this is a harmful distinction (complicates the<br>
language hard to explain etc). I think it is simply an invalid<br>
distinction and should be abolished.<br>
<br>
PEP 479 proposes to change the way that generators handle<br>
StopIteration fall-through by default. However the iterator protocol<br>
has never formally specified that an iterator should allow<br>
StopIteration to fall through to a grandparent consumer. I considered<br>
the fact that StopIteration fall-through was possible to be a<br>
convenient design feature but it was never a required part of the<br>
definition of an iterator.<br>
<br>
Many iterators perform other kinds of exception handling without<br>
allowing that to fall through and many iterators catch StopIteration<br>
from child iterators without reraising it. This has never lead anyone<br>
to suggest that such iterators are not true iterators in any way.<br>
<br>
Under PEP 479 generators will handle a StopIteration arising from the<br>
executing frame differently. The generator will still be an object<br>
with __iter__ and __next__ that exposes StopIteration at the<br>
appropriate time to its parent iterator-consumer. In other words it<br>
will still satisfy the definition of an iterator.<br>
<br>
The PEP says:<br>
<br>
"""<br>
Under this proposal, generators and iterators would be distinct, but<br>
related, concepts. Like the mixing of text and bytes in Python 2, the<br>
mixing of generators and iterators has resulted in certain perceived<br>
conveniences, but proper separation will make bugs more visible.<br>
""""<br>
<br>
This is just plain false. Under the proposal generators will still be<br>
iterators. Mixing generators and iterators is nothing like mixing text<br>
and bytes and never has been. Mixing iterators and iterables is a bit<br>
like mixing text and the bytes in the sense that it can seem to work<br>
but then sometimes silently do the wrong thing. Mixing generators and<br>
iterators is like mixing sets and containers: there is no mixing since<br>
it is simply a false distinction.<br>
<br>
AFAICT from reading through the discussions this idea has (implicitly)<br>
followed from the following fallacious argument:<br>
<br>
1) Bare next creates a problem for generators.<br>
2) Therefore we fix it by changing generators.<br>
3) Therefore generators are not iterators any more.<br>
<br>
Anyone with experience in propositional logic can see multiple<br>
fallacies in that argument but actually the biggest mistake is simply<br>
in the opening premise: bare next is a problem for all iterators.<br>
Generators are affected precisely because they are iterators.<br>
<br>
Generators were introduced as "a kind of Python iterator, but of an<br>
especially powerful kind" (PEP 255). The coroutine idea has since<br>
turned generators into something of a Frankenstein concept but the<br>
fundamental fact that they are iterators remains unchanged. And<br>
regardless of how much discussion coroutines generate the fact remains<br>
that 99% of generators are used purely for iteration.<br>
<br>
I propose to abolish this notion that generators are not iterators and<br>
to amend the text of the PEP to unambiguously state that generators<br>
are iterators regardless of any changes to the way they propagate<br>
StopIteration from the executing frame.<br>
<br>
<br>
Oscar<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><span style="color:rgb(0,102,0)">Juancarlo </span><b style="color:rgb(0,102,0)">AƱez</b></div>
</div>