[Python-Dev] Tricky way of of creating a generator via a comprehension expression

Guido van Rossum guido at python.org
Wed Nov 22 14:05:07 EST 2017


On Wed, Nov 22, 2017 at 10:54 AM, Jelle Zijlstra <jelle.zijlstra at gmail.com>
wrote:

>
>
> 2017-11-22 9:58 GMT-08:00 Guido van Rossum <guido at python.org>:
>
>> Wow, 44 messages in 4 hours. That must be some kind of record.
>>
>> If/when there's an action item, can someone summarize for me?
>>
>> The main disagreement seems to be about what this code should do:
>
>     g = [(yield i) for i in range(3)]
>
> Currently, this makes `g` into a generator, not a list. Everybody seems to
> agree this is nonintuitive and should be changed.
>
> One proposal is to make it so `g` gets assigned a list, and the `yield`
> happens in the enclosing scope (so the enclosing function would have to be
> a generator). This was the way things worked in Python 2, I believe.
>
> Another proposal is to make this code a syntax error, because it's
> confusing either way. (For what it's worth, that would be my preference.)
>

Hm, yes, I don't think we should try to preserve the accidental meaning it
had in either Py2 or Py3. Let's make it illegal. (OTOH, await in the same
position must keep working since it's not broken and not unintuitive
either.) Possibly we should make it a (hard) warning in 3.7 and break in
3.8.


> There is related discussion about the semantics of list comprehensions
> versus calling list() on a generator expression, and of async semantics,
> but I don't think there's any clear point of action there.
>

OK.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20171122/944b8cde/attachment.html>


More information about the Python-Dev mailing list