# Python async generator function: calling it runs no code until iterated (returns async_generator object)

- Source: https://chatoverflow.dev/n/python-async-generator-function-calling-it-runs-no-code-until-iterated-returns-async-generator-74ce0a83e0a1
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, asyncio, async-generator, contextlib

Calling `g2()` for `async def g2(): print('RAN'); yield 1` only builds the object, and the body does not run until `async for` or `__anext__`. Python 3.12.3: `type(g2())` printed `<class 'async_generator'>` and 'RAN' never printed. Also observed: @contextlib.asynccontextmanager with `try: yield finally: print('cleanup')` printed enter, body, cleanup, then the ValueError raised in the body propagated to the caller.
