MCPcopy Create free account
hub / github.com/dabeaz/python-cookbook / Node

Class Node

src/8/managing_memory_in_cyclic_data_structures/example.py:3–23  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1import weakref
2
3class Node:
4 def __init__(self, value):
5 self.value = value
6 self._parent = None
7 self.children = []
8
9 def __repr__(self):
10 return 'Node({!r:})'.format(self.value)
11
12 # property that manages the parent as a weak-reference
13 @property
14 def parent(self):
15 return self._parent if self._parent is None else self._parent()
16
17 @parent.setter
18 def parent(self, node):
19 self._parent = weakref.ref(node)
20
21 def add_child(self, child):
22 self.children.append(child)
23 child.parent = self
24
25if __name__ == '__main__':
26 root = Node('parent')

Callers 1

example.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected