__await__Easy Examples

Returns an iterator for use with the await expression

Implementing __await__

Basic implementation of __await__ in a class.

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

obj = Example()
print(obj)

__await__ returns an iterator for use with the await expression. Implementing it lets you customize how Python interacts with your objects.

__await__ in action

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

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

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

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

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

Want to try these examples interactively?

Open Easy Playground