[Python-ideas] Using yield inside a comprehension.

Chris Angelico rosuav at gmail.com
Tue Nov 26 10:14:02 EST 2013


On Wed, Nov 27, 2013 at 1:26 AM, Chris Angelico <rosuav at gmail.com> wrote:
> On Wed, Nov 27, 2013 at 12:54 AM, Jonathan Slenders
> <jonathan at slenders.be> wrote:
>> Where do I find the PEP that describes that the following statement assigns
>> a generator object to `values`?
>> values = [ (yield x) for x in range(10) ]
>>
>> I assume it's equivalent to the following:
>> values = (x for x in range(10))
>
> No, it's not the same; after yielding the ten values, the first one
> then _returns_ the list. It's a weird and confusing syntax to use for
> such a thing, but fundamentally it's (more or less) this:

Doh, when am I going to learn to check which list something was posted
on before I respond? SIGH. Sorry all.

But hey. Maybe the post will have been of interest to someone. It's a
fun quirk of Python.

ChrisA



More information about the Python-list mailing list