Skip to content

Latest commit

 

History

History
30 lines (24 loc) · 935 Bytes

20220908171538.md

File metadata and controls

30 lines (24 loc) · 935 Bytes

zip() got a strict keyword arg

Nice to see zip() got a "strict" keyword argument since 3.10: https://docs.python.org/3/library/functions.html#zip

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.

A nice example of the Zen of #Python's:

Errors should never pass silently.
Unless explicitly silenced.
>>> letters = ("a", "b")
>>> numbers = (1, 2, 3)
>>> zip(letters, numbers)
<zip object at 0x10c04c680>
>>> list(zip(letters, numbers))  # oops
[('a', 1), ('b', 2)]
>>> # new in 3.10
>>> zip(letters, numbers, strict=True)
<zip object at 0x10c04c8c0>
>>> # you need to materialize for it to raise
>>> list(zip(letters, numbers, strict=True))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: zip() argument 2 is longer than argument 1

#built-ins #Zen