Advice needed on __del__

André Roberge andre.roberge at gmail.com
Mon May 9 21:49:06 EDT 2005


Scott David Daniels wrote:
> André Roberge wrote:
> 
>>...  Each time I refresh the screen, I could
>>force that call, then check to see if Evil has been
>>destroyed by Python, which would give me the information
>>I need to destroy Evil_twin behind the scene myself -
>>which is what I am really after.
> 
> 
> What might work is to keep a weakref to Evil in Evil_twin.
> Upon refresh, you could check if the weakref has gone stale.
> Is that good enough?

It might be I guess ... but I don't know how to keep a weakref.
(Whips out Python in a Nutshell... yes, it's there :-)

Hmmm... The way my program is structured, Evil_twin gets created
immediately *after* Evil is created.  If it were the reverse, I think I 
could do it, but I am really not sure in this case.

However, I did try it with __del__ as I described, and, after a bit of 
fiddling got it to work perfectly.  I understand about the possible 
platform dependence issue.  However (famous last words follow...) I feel 
it's not going to bite me this time.  The main reason is that, when Evil 
is gone, before the screen gets updated, lots of drawing calls and 
wxYield() calls are made, so that __del__ has lots of time to get called.

===========
For the curious, here's how I do it:
(Sorry, no Jekyll and Hyde here :-(

class UsedRobot(object):
   def __init__(self, ..., parent=Visible_world()):
      # Visible_world is a Singleton - see Python cookbook
      true_robot = parent.addOneRobot(...)    # <---Evil_twin created
      self.robot = parent.robot_dict[true_robot.name]
      self.name = true_robot.name
      self.program = rur_program() # Singleton
      self.parent = parent
      self.parent.object_dict[self.name] = True  # <--- new trick
      self.program.wait_update_refresh(self.robot, self.name)
   def __del__(self):
      self.parent.object_dict[self.name] = False

==== class Visible_world() stuff below:
# lots os stuff omitted
     def DoDrawing(self):

    # start doing lots of background stuff
    # enough time to call __del__ ?

         self.world_image = wxEmptyBitmap(self.maxWidth, self.maxHeight)
         dc = wxMemoryDC()
         dc.SelectObject(self.world_image)
         dc.Clear()
         dc.BeginDrawing()
         self.DrawBackground(dc)
         self.DrawWalls(dc)
         self.DrawBeepers(dc)

     # ok, were we go with the important stuff for this post

         list_to_ignore = []
         for item in self.object_dict:  # see "new trick" above
             if not self.object_dict[item]:
                 list_to_ignore.append(item)

         for name in self.robot_dict:
             if name not in list_to_ignore:
                 self.DrawRobot(dc, name)
         dc.EndDrawing()
         self.updateImage = True




More information about the Python-list mailing list