The __await__()
magic method defines the behavior of an awaitable object. An awaitable object is used to implement asynchronous behavior in Python.
For example, you can implement an asynchronous function that waits for the data based to access some data like so (see source):
async def read_data(db): data = await db.fetch('SELECT ...')
Syntax __await__()
object.__await__(self)
The __await__()
method must return an iterator. Otherwise, it raises a TypeError
.
What is await in Python?
Say, you call await do_something()
in the body of the function f
. In this case, function f
will suspend its execution while do_something()
runs. As soon as the do_something()
terminates, the event loop — responsible for running asynchronous tasks — will resume the execution of function f
and will pass the result to the calling environment.
The behavior of the await keyword on the awaitable object (in our case do_something()
) can be defined using the __await__()
method.
“A coroutine waiting for a Future-like object is suspended until the Future-like object’s __await__()
completes, and returns the result.” (source)
? You can learn more about the concept of coroutines here.

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.