Searching equivalent to C++ RAII or deterministic destructors
rylesny at gmail.com
Thu Jul 2 21:03:59 EDT 2009
> You can go ahead and implement a __del__() method. It will often work in
> CPython, but you get no guarantees, especially when you have reference
> cycles and with other Python implementations that don't use refcounting.
And for resources whose lifetime is greater than your process (e.g. a
file), you can also use the atexit module to help ensure they are
cleaned/reclaimed at shutdown. One way to do this is to maintain a
weak dictionary (from the weakref module) to your objects and install
a handler which iterates this. This is useful for not only dealing
with reference cycles, but for objects which may exist at the time the
interpreter exits. These may not be deleted.
More information about the Python-list