__aiter__Easy Examples

Returns an async iterator from an async iterable

Implementing __aiter__

Basic implementation of __aiter__ in a class.

python
class Example:
    def __aiter__(self):
        return "Example __aiter__"

obj = Example()
print(obj)

__aiter__ returns an async iterator from an async iterable. Implementing it lets you customize how Python interacts with your objects.

__aiter__ in action

Seeing __aiter__ called by Python's built-in operations.

python
# How Python calls __aiter__ automatically
class Demo:
    def __init__(self, value):
        self.value = value

    def __aiter__(self):
        print(f"__aiter__ was called!")
        return self

d = Demo(42)
# This triggers __aiter__:
print(d)

Python automatically calls __aiter__ when you use the corresponding operator or function on your object.

Want to try these examples interactively?

Open Easy Playground