Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

aiter()

Obtains the asynchronous iterator used to consume an asynchronous iterable.

Version: Python 3.14; repository image, production runtime not attested

Syntax

aiter(async_iterable, /)

Obtains the asynchronous iterator used to consume an asynchronous iterable.

An asynchronous iterator returns itself on another iterator request. Fetching a value still requires an await.

The async generator implements __aiter__ and returns itself as its asynchronous iterator. Creating the iterator does not await a value. Awaiting anext fetches the first yield, 7, and aclose releases the generator. A synchronous list alone does not implement this protocol.

import asyncio
async def gen():
    yield 7
async def check():
    g = gen()
    assert aiter(g) is g and await anext(g) == 7
    await g.aclose()
asyncio.run(check())

Examples

import asyncio

async def stream():
    yield 7

async def main():
    items = aiter(stream())
    print(aiter(items) is items)
    print(await anext(items))
    await items.aclose()

asyncio.run(main())

Result

True
7

The async generator implements `__aiter__` and returns itself as its asynchronous iterator. Creating the iterator does not await a value. Awaiting anext fetches the first yield, 7, and aclose releases the generator. A synchronous list alone does not implement this protocol.

Sources