Skip to content

Commit eec6407

Browse files
committed
Doc: Clarify flattening and bugfix for itertools.tee in 3.13
1 parent 046a4e3 commit eec6407

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Doc/library/itertools.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,29 @@ loops that truncate the stream.
730730
produced by the upstream :func:`tee` call. This "flattening step"
731731
allows nested :func:`tee` calls to share the same underlying data
732732
chain and to have a single update step rather than a chain of calls.
733+
734+
.. note::
735+
736+
:func:`tee` automatically "flattens" existing tee objects,
737+
sharing the same underlying buffer instead of nesting them, to avoid
738+
performance degradation. This flattening behavior has existed since Python 3.7.
739+
740+
.. versionchanged:: 3.13
741+
Fixed a bug where re-teeing the first iterator did not correctly flatten
742+
the iterator chain in all cases. Previously, this could lead to unnecessary
743+
nesting and performance degradation in rare scenarios.
744+
745+
.. doctest::
746+
747+
>>> it = iter([1, 2, 3])
748+
>>> a, b = tee(it)
749+
>>> c, d = tee(a)
750+
>>> list(b)
751+
[1, 2, 3]
752+
>>> list(c)
753+
[1, 2, 3]
754+
>>> list(d)
755+
[1, 2, 3]
733756

734757
The flattening property makes tee iterators efficiently peekable:
735758

0 commit comments

Comments
 (0)