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

Class Node

src/4/easy_implementation_of_the_iterator_protocol/example.py:5–22  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3# Example of depth-first search using a generator
4
5class Node:
6 def __init__(self, value):
7 self._value = value
8 self._children = []
9
10 def __repr__(self):
11 return 'Node({!r})'.format(self._value)
12
13 def add_child(self, node):
14 self._children.append(node)
15
16 def __iter__(self):
17 return iter(self._children)
18
19 def depth_first(self):
20 yield self
21 for c in self:
22 yield from c.depth_first()
23
24# Example
25if __name__ == '__main__':

Callers 1

example.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected