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

Sven R. Kunze srkunze at mail.de
Thu Nov 23 02:51:44 EST 2017


On 23.11.2017 08:38, Ivan Levkivskyi wrote:
> I think this code should be just equivalent to this code
>
>     def g():
>         temp = [(yield i) for i in range(10)]
>         return (v for v in temp)
>
> Semantics of the comprehension should be clear here (just an 
> equivalent for-loop without name leaking)

Excuse me if I disagree here. If I were to understand this in real-world 
code, I cannot imagine what will happen here.

A "yield" within a comprehension is like a "return" in a comprehension. 
It makes no sense at all.
Also a "yield" and a "return with value" is also rarely seen.

Comprehensions build new objects, they are not for control flow, IMO.

Cheers,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20171123/844f3700/attachment.html>


More information about the Python-Dev mailing list