mirror of
https://github.com/python/cpython.git
synced 2025-08-21 09:21:18 +00:00
[3.12] Minor improvements to the docs for itertools.tee() (gh-119135) (gh-119137)
This commit is contained in:
parent
fa359dfe07
commit
a370eebdd9
1 changed files with 11 additions and 10 deletions
|
@ -690,18 +690,19 @@ loops that truncate the stream.
|
||||||
|
|
||||||
def tee(iterable, n=2):
|
def tee(iterable, n=2):
|
||||||
iterator = iter(iterable)
|
iterator = iter(iterable)
|
||||||
empty_link = [None, None] # Singly linked list: [value, link]
|
shared_link = [None, None]
|
||||||
return tuple(_tee(iterator, empty_link) for _ in range(n))
|
return tuple(_tee(iterator, shared_link) for _ in range(n))
|
||||||
|
|
||||||
def _tee(iterator, link):
|
def _tee(iterator, link):
|
||||||
|
try:
|
||||||
while True:
|
while True:
|
||||||
if link[1] is None:
|
if link[1] is None:
|
||||||
try:
|
link[0] = next(iterator)
|
||||||
link[:] = [next(iterator), [None, None]]
|
link[1] = [None, None]
|
||||||
except StopIteration:
|
|
||||||
return
|
|
||||||
value, link = link
|
value, link = link
|
||||||
yield value
|
yield value
|
||||||
|
except StopIteration:
|
||||||
|
return
|
||||||
|
|
||||||
Once a :func:`tee` has been created, the original *iterable* should not be
|
Once a :func:`tee` has been created, the original *iterable* should not be
|
||||||
used anywhere else; otherwise, the *iterable* could get advanced without
|
used anywhere else; otherwise, the *iterable* could get advanced without
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue