[Python-ideas] PEP 530: Asynchronous Comprehensions

Nick Coghlan ncoghlan at gmail.com
Wed Sep 7 07:31:03 EDT 2016


On 4 September 2016 at 09:31, Yury Selivanov <yselivanov.ml at gmail.com> wrote:
> With the proposed asynchronous comprehensions syntax, the above code
> becomes as short as::
>
>     result = [i async for i in aiter() if i % 2]

After using it a few times in examples, while I'm prepared to accept
the agrammatical nature of "async for" in the statement form (where
the adjective-noun phrase can be read as a kind of compound noun
introducing the whole statement), I think for the comprehension form,
we should aim to put the words in the right grammatical order if we
can:

    result = [i for i in async aiter() if i % 2]

I think the readability gain from that approach becomes even clearer
with nested loops:

    result = [x for aiterable in async outer() for x in async aiterable]

vs the current:

    result = [x async for aiterable in outer() async for x in async aiterable]

In the first form, "async" is clearly a pre-qualifier on "outer()" and
"aiterable", indicating they need to be asynchronous iterators rather
than synchronous ones.

By contrast, in the current form, the first "async" reads like a
post-qualifer on "x" (it isn't, it modifies how outer() is handled in
the outer loop), while the second looks like a post-qualifier on
"outer()" (it isn't, it modified how aiterable is handled in the inner
loop)

If that means having to postpone full async comprehensions until
"async" becomes a proper keyword in 3.7 and only adding "await in
comprehensions and generator expressions" support to 3.6, that seems
reasonable to me

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list