Skip to content

Non-string node names

In the previous example, the nodes have all been given strings as keys. This is not a requirement, and in fact any object that could be used as a key in a dictionary can be a key for a node. As function parameters can only be strings, we have to rely on the kwds argument to add_node to specify which nodes should be used as inputs for calculation nodes' functions. For a simple but frivolous example, we can represent a finite part of the Fibonacci sequence using tuples of the form ('fib', [int]) as keys:

>>> comp = Computation()
>>> comp.add_node(('fib', 1), value=1)
>>> comp.add_node(('fib', 2), value=1)
>>> for i in range(3,7):
...    comp.add_node(('fib', i), lambda x, y: x + y, kwds={'x': ('fib', i - 1), 'y': ('fib', i - 2)})
...
>>> comp.draw()

%3n0('fib', 1)n2('fib', 3)n0->n2n1('fib', 2)n1->n2n3('fib', 4)n1->n3n2->n3n4('fib', 5)n2->n4n3->n4n5('fib', 6)n3->n5n4->n5

>>> comp.compute_all()
>>> comp.value(('fib', 6))
8