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

Ivan Levkivskyi levkivskyi at gmail.com
Sat Nov 25 11:07:43 EST 2017


On 25 November 2017 at 16:57, Guido van Rossum <guido at python.org> wrote:

> On Sat, Nov 25, 2017 at 6:55 AM, Ivan Levkivskyi <levkivskyi at gmail.com>
> wrote:
>
>> On 25 November 2017 at 04:30, Guido van Rossum <guido at python.org> wrote:
>>
>>> On Fri, Nov 24, 2017 at 4:22 PM, Guido van Rossum <guido at python.org>
>>> wrote:
>>>
>>>> The more I hear about this topic, the more I think that `await`,
>>>> `yield` and `yield from` should all be banned from occurring in all
>>>> comprehensions and generator expressions. That's not much different from
>>>> disallowing `return` or `break`.
>>>>
>>>
>>> From the responses it seems that I tried to simplify things too far.
>>> Let's say that `await` in comprehensions is fine, as long as that
>>> comprehension is contained in an `async def`. While we *could* save `yield
>>> [from]` in comprehensions, I still see it as mostly a source of confusion,
>>> and the fact that the presence of `yield [from]` *implicitly* makes the
>>> surrounding `def` a generator makes things worse. It just requires too many
>>> mental contortions to figure out what it does.
>>>
>>
>> [...]
>> If the first example will be allowed, then one will be surprised why it
>> can't be rewritten as
>>
>>     def pack_two():
>>         return [(yield) for _ in range(2)]
>>
>
> And yet Nick's example shows that that is not equivalent!
>
> [...]
>
> In this example each thing that looks syntactically like a list
> comprehension becomes actually a generator expression at at runtime! And so
> does your example, so instead of a list of two items, it returns a
> generator that will produce two values when iterated over.
>
> That's not referential transparency to me, it feels more like a bug in the
> code generator.
>
> I want to ban this because apparently nobody besides Nick knows about this
> behavior (I certainly didn't, and from the above it seems you don't either).
>

This whole thread started as a proposal to fix this bug and to make the two
forms equivalent, so I don't know what you are talking about.

Also as there appeared arguments of authority (thanks Antoine) its time to
stop this discussion for me.

--
Ivan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20171125/9fcc68b9/attachment.html>


More information about the Python-Dev mailing list