1

The following code got the error of TypeError: 'async_generator' object is not iterable

async def f(i):
    return i*10, i*100

l2 = [1,2,3]
[x for x in ((i, await f(i)) for i in l2)]

However, it works if not using generator?

[x for x in [(i, await f(i)) for i in l2]]

1 Answer 1

1

Introducing the async keyword in the generator expression makes it become an async generator.

If you are using a recent Ipython as the REPL, when used as a list comprehension, the expression in the inner list is fully resolved.. Ipython special "call async functions in a transparent way" do just that, and all the outer loop "sees" is simply a list.

In normal Python code, or on the "pure" REPL, the async keyword use is restricted to async function bodies, and you'd get a SyntaxError: asynchronous comprehension outside of an asynchronous function error with both snippets anyway.

So, getting back to your problem: introducing an await in the expression in the inner generator, it becomes an asynchronous generator - the outer code will need the inner code to be run on each iteration. But all you need is to use async for to get its values in the outer comprehension:

[x async for x in [(i, await f(i)) for i in l2]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.