About 1392 letters

About 7 minutes

#aiter

Description: Obtain an asynchronous iterator from an asynchronous iterable (by calling its __aiter__ method). See also the iter function.

def aiter(async_iterable):
    '''
    Obtain an asynchronous iterator from an asynchronous iterable.

    :param async_iterable: An asynchronous iterable object
    :return: An asynchronous iterator of the argument
    '''

Example:

import asyncio

# Asynchronous iterator
class AsyncIterator:
    def __init__(self, stop):
        self.__stop = stop
        self.__current = 0
    
    async def __anext__(self):
        if self.__current < self.__stop:
            await asyncio.sleep(0.1)  # Simulate asynchronous operation
            self.__current += 1
            return self.__current - 1
        else:
            raise StopAsyncIteration

# Asynchronous iterable
class AsyncIterable:
    def __init__(self, stop):
        self.__iterator = AsyncIterator(stop)

    def __aiter__(self):
        return self.__iterator

# Create asynchronous iterable object
async_iterable = AsyncIterable(10)

# Obtain iterator
async_iterator = aiter(async_iterable)
print(async_iterator)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

Updated: 6/9/2025

Author: PlanC

Created: 6/9/2025