Visitar URL original
Misc improvements to the itertools docs by rhettinger · Pull Request #119040 · python/cpython · GitHub
Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use contextlib.suppress() instead of try/except
  • Loading branch information
rhettinger committed May 13, 2024
commit c4bfbfb252b9162fb79a09d23793f9f2b4998164
9 changes: 3 additions & 6 deletions Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,7 @@ and :term:`generators <generator>` which incur interpreter overhead.
.. testcode::

import collections
import contextlib
import functools
import math
import operator
Expand Down Expand Up @@ -949,23 +950,19 @@ and :term:`generators <generator>` which incur interpreter overhead.
else:
stop = len(iterable) if stop is None else stop
i = start
try:
with contextlib.suppress(ValueError):
while True:
yield (i := seq_index(value, i, stop))
i += 1
except ValueError:
pass

def iter_except(func, exception, first=None):
"Convert a call-until-exception interface to an iterator interface."
# iter_except(d.popitem, KeyError) → non-blocking dictionary iterator
try:
with contextlib.suppress(exception):
if first is not None:
yield first()
while True:
yield func()
except exception:
pass


The following recipes have a more mathematical flavor:
Expand Down