Skip to content

Commit fcbf9b1

Browse files
authored
Docs: Clarify the before_and_after() example (pythonGH-28458)
1 parent a677971 commit fcbf9b1

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

Doc/library/itertools.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -859,10 +859,11 @@ which incur interpreter overhead.
859859
""" Variant of takewhile() that allows complete
860860
access to the remainder of the iterator.
861861

862-
>>> all_upper, remainder = before_and_after(str.isupper, 'ABCdEfGhI')
863-
>>> str.join('', all_upper)
862+
>>> it = iter('ABCdEfGhI')
863+
>>> all_upper, remainder = before_and_after(str.isupper, it)
864+
>>> ''.join(all_upper)
864865
'ABC'
865-
>>> str.join('', remainder)
866+
>>> ''.join(remainder) # takewhile() would lose the 'd'
866867
'dEfGhI'
867868

868869
Note that the first iterator must be fully

Lib/test/test_itertools.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2646,10 +2646,11 @@ def test_permutations_sizeof(self):
26462646
>>> list(odds)
26472647
[1, 3, 5, 7, 9]
26482648
2649-
>>> all_upper, remainder = before_and_after(str.isupper, 'ABCdEfGhI')
2650-
>>> str.join('', all_upper)
2649+
>>> it = iter('ABCdEfGhI')
2650+
>>> all_upper, remainder = before_and_after(str.isupper, it)
2651+
>>> ''.join(all_upper)
26512652
'ABC'
2652-
>>> str.join('', remainder)
2653+
>>> ''.join(remainder)
26532654
'dEfGhI'
26542655
26552656
>>> list(powerset([1,2,3]))

0 commit comments

Comments
 (0)