looking for a pattern

Peter Hansen peter at engcorp.com
Fri Feb 7 14:51:17 EST 2003


Geoff Gerrietts wrote:
> 
> I'm looking for a pattern. I don't have one in my mental filing
> cabinet yet, and I'm not sure how to start hunting for it, so I
> figured my first stop would be to ask here.
> 
> Assume I have a hierarchy of instances, grandpa -> papa -> boy, with the
> standard biological constraints (one male parent, zero or more sibs).
> I want boy to know who his papa is (sue me, I'm traditional that way),
> as well as vice versa.
> 
> Now, the "traditional" way to do this would be through a backlink --
> boy has an ivar parent that gets hooked up at instantiation time. But
> this creates a cycle, which I know to be a Bad Thing in my unfortunate
> 1.5.2 world.
> 
> Is there a better way to solve this problem that doesn't create a
> cycle, or is the backlink the best bet? For that matter, with the
> advent of the new garbage collection, is it even worth avoiding cycles
> in newer versions of the interpreter?

You said 1.5.2, but now you're talking about newer versions of the 
interpreter.  If you really allow for the possibility of using newer
versions of the interpreter, then weakrefs would solve your problem
and the answer would be "it is worth avoiding cycles, by using weakrefs".

-Peter




More information about the Python-list mailing list