I'm currently learning the graphlib of python 3.9. And I want to generate a CycleError but when I run the following code:
#!/usr/bin/python3
from graphlib import TopologicalSorter
ts = TopologicalSorter()
ts.add(2, 1)
ts.add(3, 2)
ts.add(4, 3)
ts.add(1, 4)
ts.static_order()
I don't get a CycleError. But if I change the code like this:
#!/usr/bin/python3
from graphlib import TopologicalSorter
ts = TopologicalSorter()
ts.add(2, 1)
ts.add(3, 2)
ts.add(4, 3)
ts.add(1, 4)
tuple(ts.static_order()) # why does this work with tuple or list ?
It works but why ?
TopologicalSorter.static_order()
is generator; function body contains yield
statement, then the return value of function call is iterator.
Function body won't executed at all before very first attempt of extracting element from the iterator.