Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

CKTIS / Python

aiter()

Obtiene un iterador asíncrono mediante `__aiter__` sin esperar un valor.

Versión: Python 3.14; repository image, production runtime not attested

Sintaxis

aiter(async_iterable, /)

Obtiene un iterador asíncrono mediante __aiter__ sin esperar un valor.

El generador se devuelve a sí mismo y la prueba de identidad da True. Solo await anext obtiene 7 y aclose cierra el generador.

Una lista ordinaria no admite este protocolo.

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

Ejemplos

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

Resultado

True
7

Una lista ordinaria no admite este protocolo.

Fuentes