[Python-Dev] Return from generators in Python 3.2
Yury Selivanov
yselivanov at gmail.com
Fri Aug 27 00:31:52 CEST 2010
On 2010-08-26, at 6:11 PM, Nick Coghlan wrote:
> On Fri, Aug 27, 2010 at 1:00 AM, Yury Selivanov <yselivanov at gmail.com> wrote:
>> In the above, GeneratorReturn error will be propagated stopping the program execution.
>> Strictly speaking, the proposed change is just alters the current Python behaviour,
>> making the 'return value' statement raise catchable error (instead of SyntaxError.)
>
> There are fairly extensive discussions of using a new GeneratorReturn
> exception rather than StopIteration in the python-dev archives. As I
> recall, one key factor leading to the use of StopIteration was the
> suggestion's implied breakage of the equivalence between "return"
> (which would continue to raise StopIteration) and "return None" (which
> would raise GeneratorReturn with a value of None). Using a different
> exception also made all generator handling code clumsier, since it now
> needed to deal with two exceptions rather than just one.
Yes, I understand the point of having two different exceptions for
basically two close related things - 'return None' and 'return value'.
However, as I outlined in the first message, this was intended to prevent
this kind of mistakes:
... def test():
... for i in range(10):
... yield i
... return 10
Which will certainly happen, especially with people new to python.
Again, this exception will be used in very specific places by a very
specific software, that expects it. Otherwise, it should be propagated
and crash the whole thing.
As for the generator handling code -- are you sure there is that much
of it?
> Since the only situations where a return value could be inadvertently
> ignored were those where the application clearly didn't care about the
> return value anyway, it was decided that sticking with a single
> exception type was the better approach.
Good point. It's all about our level of care about beginners ;)
> PEP 380 should probably mention this idea explicitly though, since
> using a new exception type is a fairly obvious alternative suggestion
> and the discussion of the idea is scattered all over the place in the
> archives.
>
> As for breaking the moratorium for it - no, not even close to a big
> enough win, since people can already write "raise
> CoroutineReturn(result)".
Well, people certainly can. But the goal is to make convenient instruments
for everyday use. I, for example, deal with really a lot of coroutine based
code, and it's very annoying that I have to use some creepy abstractions
in order to just return a value! It's especially annoying when you have
normal code with normal returns and coroutines, with 'return_ (value)'.
And I don't think it frustrates just me. Coroutines protocol is incomplete
and there is a very little action required to fix it.
All this proposal is suggesting is to replace SyntaxError with
GeneratorReturn (or StopIteration). I'd classify is as a minor change
than some special refactoring that may fall under the moratorium. Correct
me if I'm wrong.
-
Yury
More information about the Python-Dev
mailing list