async def
`async def` defines a function whose call does not run the body but returns a coroutine that someone must wait for with `await` or run with `asyncio.run()`.
Used in 15 lessons of the course, from 01.05.
You write async def instead of def to create a coroutine function. The signature reads like a normal function's: parameters, type hints and -> with the return type.
The difference is in the call. If you call slow_answer_async("hi"), the body does not run yet. Python hands you a coroutine object, like a pending task. To make it run, you wait for it with await inside another async def, or pass it to asyncio.run() from regular code.
It is written this way because await marks the points where the function may pause and let other work move on, for example while waiting for a model's reply. You can only use await inside an async def.
In the course, async def slow_answer_async(question: str) -> str: is the async version of a function that returns a str. That -> str describes what you get after the await, not the coroutine itself.
Reach for it when: the function waits on something (a network, a model) and you want to use `await` inside it or let other tasks progress meanwhile.
Examples
Common mistake
async def get() -> str:
return "hi"
result = get()
print(result.upper())Why: Calling `get()` returns a coroutine, not a `str`. A coroutine has no `.upper()`.
How to write it: Wait for the result: result = await get() inside another async def, or asyncio.run(get()).
