Skip to content

Commit

Permalink
pythonGH-100101: Clarify documentation of zip's strict option (python…
Browse files Browse the repository at this point in the history
…GH-100103)

(cherry picked from commit cf1c098)

Co-authored-by: JustAnotherArchivist <JustAnotherArchivist@users.noreply.github.com>
  • Loading branch information
miss-islington and JustAnotherArchivist authored Dec 28, 2022
1 parent 9120450 commit ac31120
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions Doc/library/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1918,14 +1918,24 @@ are always available. They are listed here in alphabetical order.
>>> list(zip(('a', 'b', 'c'), (1, 2, 3), strict=True))
[('a', 1), ('b', 2), ('c', 3)]

Unlike the default behavior, it checks that the lengths of iterables are
identical, raising a :exc:`ValueError` if they aren't:

>>> list(zip(range(3), ['fee', 'fi', 'fo', 'fum'], strict=True))
Unlike the default behavior, it raises a :exc:`ValueError` if one iterable
is exhausted before the others:

>>> for item in zip(range(3), ['fee', 'fi', 'fo', 'fum'], strict=True): # doctest: +SKIP
... print(item)
...
(0, 'fee')
(1, 'fi')
(2, 'fo')
Traceback (most recent call last):
...
ValueError: zip() argument 2 is longer than argument 1

..
This doctest is disabled because doctest does not support capturing
output and exceptions in the same code unit.
https://github.com/python/cpython/issues/65382
Without the ``strict=True`` argument, any bug that results in iterables of
different lengths will be silenced, possibly manifesting as a hard-to-find
bug in another part of the program.
Expand Down

0 comments on commit ac31120

Please sign in to comment.