fork()

Hisao Suzuki suzuki611 at okisoft.co.jp
Mon Jun 14 23:04:43 EDT 1999


In article <7k35bf$13iq$1 at nntp6.u.washington.edu>,
donn at u.washington.edu (Donn Cave) wrote:
> I want to ask more about this, partly I'm not sure I understand what
> the trees and their roots are or what registration would require.

I'm sorry for my poor explanation.  The following is a simple
example of a doubly-linked tree.

	class Node: pass
	root, node1, node2 = Node(), Node(), Node()
	root.child1, node1.parent = node1, root
	root.child2, node2.parent = node2, root

Having circular references, the nodes are never deleted even if
you `del root, node1, node2'.  The registration requires at
least one node: root, node1, or node2.  You can traverse the
entire tree from any node, but you would probably register the
root since it is the most convenient node to start the traverse.

In option (A), the registration also requires a function which
will be applied to the root (or the node you registered) by the
garbage collector. (The function may be a method of a certain
name and thus the registration may require only the root.)

In general, so-called `aggregations' in OO terms are implemented
with trees.  If the association of an aggregation is
bi-directional, the result is a doubly-linked tree.  I hope you
imagine more practical examples.

> Could something like a standard class method accomplish this?  Like,
> 
>    def __garbagecollected__(self):
>        # author knows self.parent is a circular reference.
>        self.parent = None
> 
>    def __del__(self):
>        if self.parent:
>            ...

Perhaps.  But I am afraid it may be a little difficult, in
practice, to write a correct __garbagecollected__ method which
must work in any order of invocations determined arbitrarily by
the garbage collector.  On the other hand, if you register the
root explicitly, you have a guarantee that the root will be
accessed first, thus the whole tree is guaranteed to be in a
more predictable state.

--===-----========------------- Sana esprimo naskas sanan ideon.
SUZUKI Hisao            suzuki611 at okisoft.co.jp, suzuki at acm.org.




More information about the Python-list mailing list