Skip to content

Commit 0a74d33

Browse files
miss-islingtonrhettinger
authored andcommitted
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 2e4d66d commit 0a74d33

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

0 commit comments

Comments
 (0)