Special Method Protocols

__aiter__() — async protocol

Return an asynchronous iterator directly when aiter(obj) or async for requests it. Since Python 3.7, returning an awaitable instead of an asynchronous iterator is not the protocol.

Formal Syntax & Structure

python syntaxpython
def __aiter__(self):

Return Value

async iterator

Object with asynchronous next method.

Working Example

Example Snippetpython
import asyncioclass Stream:    def __aiter__(self): return self    async def __anext__(self): raise StopAsyncIterationasync def demo(): return [x async for x in Stream()]print(asyncio.run(demo()))
Python Reference