tezvyn:

What is the difference between async def and regular functions?

AI-drafted, machine-checkedSource: docs.python.orgbeginner
What is the difference between async def and regular functions?

This tests async def call semantics. A strong answer contrasts regular functions, which execute immediately, with async def, which returns a coroutine object that does nothing until awaited or wrapped in create_task.

WHAT THIS TESTS: Whether the candidate understands the distinction between a coroutine function and a coroutine object, and whether they know that calling an async def function does not execute its body. The interviewer is looking for awareness of the event loop as the driver of execution and the specific mechanisms that move a coroutine from created to running.

A GOOD ANSWER COVERS: First, the candidate should state that a regular function begins executing its body immediately upon call and returns a value when complete. Second, they should explain that an async def function, when called, returns a coroutine object; the function body does not run yet. Third, they should list the three primary ways this object is executed: awaiting it directly inside another coroutine, wrapping it with asyncio.create_task to schedule it concurrently on the event loop, or using asyncio.run for the top-level entry point. Fourth, they should mention that a coroutine object is an awaitable, which is a prerequisite for these mechanisms.

COMMON WRONG ANSWERS: The biggest red flag is claiming that calling an async def function runs the code immediately like a regular function. Another mistake is saying that async def makes a function run in parallel automatically; it merely provides a construct for cooperative multitasking. Some candidates forget that a bare call without await or create_task produces an unawaited coroutine object, which triggers a RuntimeWarning and never executes. Confusing coroutine functions with Tasks or Futures is also a signal of shallow understanding.

LIKELY FOLLOW-UPS: The interviewer may ask how asyncio.create_task differs from a bare await, which opens a discussion about concurrency versus sequential execution. They might ask what a Task is, leading to the relationship between Tasks, the event loop, and the underlying Future. Another common follow-up is how to run async code from a synchronous context, which touches on asyncio.run and thread safety. Finally, they may ask about the async for and async with statements to probe comprehension of the broader async syntax.

ONE CONCRETE EXAMPLE: Consider an async def fetch_data(): return 42. If you write result = fetch_data(), result is a coroutine object, not the integer 42. The function body has not executed. To get the value, you must write value = await result inside another async function, or schedule it with task = asyncio.create_task(fetch_data()) and later await task. If you simply call fetch_data() and discard the return value, CPython warns RuntimeWarning: coroutine fetch_data was never awaited because the event loop never received the object.

Source: docs.python.org

Read the original → docs.python.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.