Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / Python

aiter()

Ottiene un iteratore asincrono tramite `__aiter__` senza attendere un valore.

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

Sintassi

aiter(async_iterable, /)

Ottiene un iteratore asincrono tramite __aiter__ senza attendere un valore.

Il generatore restituisce se stesso e il test di identità dà True. Solo await anext recupera 7 e aclose chiude il generatore.

Una lista ordinaria non supporta questo protocollo.

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())

Esempi

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())

Risultato

True
7

Una lista ordinaria non supporta questo protocollo.

Fonti