Advice needed on __del__
Scott David Daniels
Scott.Daniels at Acm.Org
Tue May 10 11:30:15 EDT 2005
André Roberge wrote:
> 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?
>
> ...
Try something like this:
++ import weakref
> class UsedRobot(object):
-- def __init__(self, ..., parent=Visible_world()):
++ def __init__(self, ..., jeckyll=None, parent=Visible_world()):
++ # need to know if we are good or evil.
> # 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)
++ if jeckyll is None:
++ # Making a good twin. Make an evil partner
++ twin = parent.addOneRobot(..., jeckyll=self)
++ self.twin = lambda: return twin # match weakref interface
++ # hold a ref to the evil twin, so it will be alive
++ else:
++ # Making an evil twin. Make a weak ref to the good
++ twin = parent.addOneRobot(..., jeckyll=self, ...)
++ self.twin = weakref.ref(jeckyll)
--> def __del__(self):
--> self.parent.object_dict[self.name] = False
...
Somewhere else when deciding to draw a particular robot:
... robot = whatever ...
--> <draw robot>
++ if robot.twin() is None:
++ # This must be an evil twin with a dead good twin
++ <avoid drawing robot>
++ else:
++ # this is either a good twin or
++ <draw robot>
...
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list