looking for a pattern

Changjune Kim juneaftn at REMOVETHIShanmail.net
Fri Feb 7 20:32:33 EST 2003


"Geoff Gerrietts" <geoff at gerrietts.net> wrote in message
news:mailman.1044653839.4696.python-list at python.org...
> Quoting Carlos Ribeiro (cribeiro at mail.inet.com.br):
> > You can use the list to hold all objects, or simply use the list to
keep a few
> > auxiliary links, but you got the idea. Of course, you are going to be
careful
> > and do the link management yourself, but this technique allows you to
avoid
> > explicit circular references.
>
> This is sorta the pattern I was looking for, I think.
>
> I can see an alternate implementation along the lines of keeping all
> the objects in a collection / dictionary by id(obj), and managing the
> parent links on the individual objects. Might look like:
>
> Collection = {}
>
> class FamilyMember:
>   def __init__(self, parent_id):
>     Collection[id(self)] = self
>     self._parent_id = parent_id
>
>   def parent(self):
>     return Collection.get(self._parent_id)
>
> I think that might be the near-equivalent of a weakref, in 1.5.2?
> Except the Collection needs to be explicitly managed.
>
> --G.

Use Mediator Pattern. It's good for managing bidirectional relationships.





More information about the Python-list mailing list