catching a del

Alex Martelli aleaxit at yahoo.com
Fri Mar 2 04:43:07 EST 2001


Dan Parisien" <dan at eevolved.com> wrote in message
news:j%zn6.322252$f36.11513719 at news20.bellglobal.com...
> class obj:
>         def __init__(self, name, parent):
>                 parent.children[name] = self
>                 self.parent = parent
>                 self.name = name
>
>         def onDelete(self): #?!
>                 del self.parent[self.name]
>
> I want to be able to catch a del of an instance of the above class so I
can
> remove it from its parent dictionary. Is there a way? (note it cannot be
in
> a destructor because there will always be a reference inside the parent
> dict)

You need a "weak reference" in the 'parent' -- one that, by itself,
will not suffice to keep the referred-to entity 'alive' if no other
references exist.  See PEP 205, "Weak References" -- its URL is
http://python.sourceforge.net/peps/pep-0205.html, and the text
includes a good general explanation and pointers to previous
work on the subject.  If you need it right now, your best bet is
probably mxProxy, http://www.lemburg.com/files/python/mxProxy.html,
but you should also explore the other references in the PEP text.


Alex






More information about the Python-list mailing list