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

Guido van Rossum guido at python.org
Sat Nov 25 11:59:49 EST 2017


On Sat, Nov 25, 2017 at 8:07 AM, Ivan Levkivskyi <levkivskyi at gmail.com>
wrote:

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

I see. I misread your equivalence example as "this how it works" -- you
meant it as "this is how I propose we fix it".

The fix is not unreasonable but I still would like to retreat to the
territory where `yield [from]` in comprehensions (and generator
expressions) is deemed invalid.


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

I'd be happy to stop with the conclusion that we're going to rip out some
confusing syntax rather than trying to generate code for it -- IMO we've
proved to ourselves that this stuff is too complicated to be useful.

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


More information about the Python-Dev mailing list