__del__ and saving/restoring dc
David Montgomery
davidlmontgomery at gmail.com
Wed Sep 19 15:33:27 PDT 2007
Hi,
I'm taking a look at Andrea Gavana's LabelBook.
Something that surprised me was the DCSaver
class was being used ... essentially, constructing
an object at the beginning of a method to save the
current dc, modifying things within the method, and
then apparently relying on __del__ to restore the
earlier state. Very C++, RAII-like.
class DCSaver:
def __init__(self, pdc):
self._pdc = pdc
self._pen = pdc.GetPen()
self._brush = pdc.GetBrush()
def __del__(self):
if self._pdc:
self._pdc.SetPen(self._pen)
self._pdc.SetBrush(self._brush)
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. It seems things might not be restored immediately,
and it seems that the __del__ restoration code might be
called in a different order than the exact reverse of the
saving calls, in which case you wouldn't get back to the
starting state.
Does DCSaver work? That is, is it guaranteed to
restore things, as it would if __del__ were a C++
destructor?
Thanks,
David
More information about the wx-users
mailing list