[Python] What happens if __del__ is defined as an async def coroutine in Python (CPython 3.12)?

Discussão em 'Python' iniciado por Stack, Agosto 30, 2025.

  1. Stack

    Stack Membro Participativo

    I'm experimenting with asynchronous patterns in Python and was curious what happens if I define a del method using async def in Python 3.12. For example:

    class MyClass:
    async def __del__(self):
    print("Async __del__ called")


    Is this valid Python syntax?

    Will the coroutine be awaited automatically when the object is garbage collected? If not awaited, is there any warning or silent failure?

    Has this behavior changed in recent versions (like 3.11 → 3.12)?

    Is there any recommended way to run async cleanup logic upon object deletion?

    Continue reading...

Compartilhe esta Página