Skip to content
Discussion options

You must be logged in to vote

This happens only if the genexp contains an async listcomp inside of it:

>>> async def foo():
...     print(x for x in [1])
...     print([a async for a in b] for x in [1])
...     
>>> asyncio.run(foo())
<generator object foo.<locals>.<genexpr> at 0x101a33a70>
<async_generator object foo.<locals>.<genexpr> at 0x101aa5130>

This makes sense because the inner listcomp is async and therefore must be evaluated within an async context. It's not clearly documented, though: https://docs.python.org/3.14/reference/expressions.html#generator-expressions says "If a generator expression contains either async for clauses or await expressions it is called an asynchronous generator expression. An async…

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@injust
Comment options

@erictraut
Comment options

@JelleZijlstra
Comment options

Answer selected by injust
@injust
Comment options

@JelleZijlstra
Comment options

@injust
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants