__del__ and saving/restoring dc

Chris Mellon arkanes at gmail.com
Thu Sep 20 09:43:04 PDT 2007


On 9/20/07, Robin Dunn <robin at alldunn.com> wrote:
> David Montgomery wrote:
>
> > But I was under the impression that one shouldn't
> > rely on __del__ for this kind of thing in python, because
> > you don't really know when __del__ is going to be
> > called.
>
> At least in CPython it will be called as soon reference count goes to
> zero.  So if there is only one reference __del__ will be called as soon
> as it goes out of scope, just like the destructor of a C++ object on the
> stack.
>

In Python 2.5, you can use the with statment with objects that support
the context manager protocol. This is an explicit implementation of
RAII, and is better in that it gives you a guarantee of the behavior
as well as letting you know if you exited normally or due to an
exception.

See http://docs.python.org/whatsnew/pep-343.html for details. They are
very easy to write, and I encourage people like Andrea to implement
the context manager interface for these sort of objects.




More information about the wx-users mailing list