Skip to content

Commit 6229ad5

Browse files
committed
Add example on insertion order affecting static_order()
1 parent ed8c4ee commit 6229ad5

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Doc/library/functools.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,29 @@ The :mod:`functools` module defines the following functions:
679679
yield from node_group
680680
self.done(*node_group)
681681

682+
The particular order that is returned may depend on the particular order in
683+
which the items were inserted in the graph. For example:
684+
685+
.. doctest::
686+
:hide:
687+
688+
>>> ts = TopologicalSorter()
689+
>>> ts.add(3, 2, 1)
690+
>>> ts.add(1, 0)
691+
>>> print([*ts.static_order()])
692+
[2, 0, 1, 3]
693+
694+
>>> ts2 = TopologicalSorter()
695+
>>> ts2.add(1, 0)
696+
>>> ts2.add(3, 2, 1)
697+
>>> print([*ts2.static_order()])
698+
[0, 2, 1, 3]
699+
700+
This is due to the fact that "0" and "2" are in the same level in the graph (they
701+
would have been returned in the same call to :meth:`~TopologicalSorter.get_ready`)
702+
and the order between them is determined by the order of insertion.
703+
704+
682705
If any cycle is detected, :exc:`CycleError` will be raised.
683706

684707
.. versionadded:: 3.9

Lib/functools.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,9 @@ def _find_cycle(self):
415415
def static_order(self):
416416
"""Returns an iterable of nodes in a topological order.
417417
418+
The particular order that is returned may depend on the particular
419+
order in which the items were inserted in the graph.
420+
418421
Using this method does not require to call "prepare" or "done". If any
419422
cycle is detected, :exc:`CycleError` will be raised.
420423
"""

0 commit comments

Comments
 (0)