Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / Python

anext()

Chama `__anext__` e devolve um objeto para aguardar com await.

Versão: Python 3.14; repository image, production runtime not attested

Sintaxe

await anext(async_iterator[, default])

Chama __anext__ e devolve um objeto para aguardar com await.

A primeira espera obtém 8 do gerador. Após o esgotamento o valor predefinido fornece o texto end e omiti-lo gera StopAsyncIteration.

Um None emitido conta como elemento real.

import asyncio
async def gen():
    yield None
async def check():
    g = gen()
    assert await anext(g, 9) is None
    assert await anext(g, 9) == 9
asyncio.run(check())

Exemplos

import asyncio

async def stream():
    yield 8

async def main():
    items = stream()
    print(await anext(items))
    print(await anext(items, "end"))

asyncio.run(main())

Resultado

8
end

Um None emitido conta como elemento real.

Fontes