Python 3.5+astterm
ast.AsyncFor
async
The node of an async for loop: the same fields as For — target, iter, body, orelse and type_comment. It is allowed only inside async def.
Syntax
ast.AsyncFor(target, iter, body, orelse, type_comment=None)
A separate class, not a subclass of For: visit_For never sees it. ast.parse accepts async for even at module level, and only compile() reports 'async for' outside async function. An async for inside a list comprehension is not AsyncFor but a comprehension with is_async=1.
Examples
import ast
fn = ast.parse('async def f():\n async for row in rows:\n pass').body[0]
loop = fn.body[0]
print(type(loop).__name__, loop.target.id, isinstance(loop, ast.For)) # → AsyncFor row False
import ast
comp = ast.parse('[x async for x in aiter_()]', mode='eval').body
print(type(comp).__name__, comp.generators[0].is_async) # → ListComp 1
See also
Open in the full glossarydocs.python.orgReport an inaccuracy