Skip to content

Commit 697b665

Browse files
Docs: Clarify the before_and_after() example (GH-28458) (#28464)
(cherry picked from commit fcbf9b1) Co-authored-by: Raymond Hettinger <[email protected]> Co-authored-by: Raymond Hettinger <[email protected]>
1 parent 0e96086 commit 697b665

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
@@ -2605,10 +2605,11 @@ def test_permutations_sizeof(self):
26052605
>>> list(odds)
26062606
[1, 3, 5, 7, 9]
26072607
2608-
>>> all_upper, remainder = before_and_after(str.isupper, 'ABCdEfGhI')
2609-
>>> str.join('', all_upper)
2608+
>>> it = iter('ABCdEfGhI')
2609+
>>> all_upper, remainder = before_and_after(str.isupper, it)
2610+
>>> ''.join(all_upper)
26102611
'ABC'
2611-
>>> str.join('', remainder)
2612+
>>> ''.join(remainder)
26122613
'dEfGhI'
26132614
26142615
>>> list(powerset([1,2,3]))

0 commit comments

Comments
 (0)