]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Minor improvements to the docs for itertools.tee() (gh-119135)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Sat, 18 May 2024 06:32:34 +0000 (01:32 -0500)
committerGitHub <noreply@github.com>
Sat, 18 May 2024 06:32:34 +0000 (01:32 -0500)
Doc/library/itertools.rst

index a19baa3f0e439ff4b9900371135723615ff777a3..6d33748898361d433ed383a42a0da88521734fc6 100644 (file)
@@ -698,18 +698,19 @@ loops that truncate the stream.
 
         def tee(iterable, n=2):
             iterator = iter(iterable)
-            empty_link = [None, None]  # Singly linked list: [value, link]
-            return tuple(_tee(iterator, empty_link) for _ in range(n))
+            shared_link = [None, None]
+            return tuple(_tee(iterator, shared_link) for _ in range(n))
 
         def _tee(iterator, link):
-            while True:
-                if link[1] is None:
-                    try:
-                        link[:] = [next(iterator), [None, None]]
-                    except StopIteration:
-                        return
-                value, link = link
-                yield value
+            try:
+                while True:
+                    if link[1] is None:
+                        link[0] = next(iterator)
+                        link[1] = [None, None]
+                    value, link = link
+                    yield value
+            except StopIteration:
+                return
 
    Once a :func:`tee` has been created, the original *iterable* should not be
    used anywhere else; otherwise, the *iterable* could get advanced without