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

Paul Moore p.f.moore at gmail.com
Sat Nov 25 10:47:14 EST 2017

On 25 November 2017 at 14:55, Ivan Levkivskyi <levkivskyi at gmail.com> wrote:
> Continuing the topic of the ban, what exactly should be banned? For example
> will this still be valid?
>     def pack_two():
>         return [(yield), (yield)]  # Just a list display
> I don't see how this is controversial. It is clear that `pack_two` is a
> generator.

It's not clear to me...

Seriously, I wouldn't know what this would do. Presumably it needs
someone calling send() to generate results (because that's now yield
expressions work) but beyond that, I don't really understand what it
does. Maybe an example that wasn't artificial would be more obvious,
I'm not sure.

> If this is going to be prohibited, then one may be surprised by lack of
> referential transparency, since this will be valid:
>     def pack_two():
>         first = (yield)
>         second = (yield)
>         return [first, second]

The fact that you can't inline first and second doesn't bother me. I
can't fully explain why, but it doesn't. (Technically, I *can* explain
why, but you'd disagree with my explanation :-))

> 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)]
> I have found several other examples where it is not clear whether they
> should be prohibited with `yield` or not.

So far, none of your examples have demonstrated anything that Guido's
suggestion to ban yield would make confusing *to me*.

Maybe this demonstrates nothing more than how inconsistent and shallow
my understanding of yield expressions is. That's fine, I can live with
that. I can't give you any assurances that my level of understanding
is common among non-experts, but I will say that in my view, Guido's
proposal feels sensible and intuitive. And after all, if the use of
yield expressions becomes significantly more common, and the general
level of familiarity with the concept increases, it's easy enough to
relax the restriction later, in line with the average user's level of


More information about the Python-Dev mailing list