[PYTHON] I read PEP 618 (Add Optional Length-Checking To zip)

The other day, PEP 618 (Add Optional Length-Checking To zip) was [committed that it was accepted](https://github. I saw com / python / peps / pull / 1435). So, this time I will read PEP 618.

Overview

  >>> list(zip([1,2,3], [4]))  # 2,3 will be lost
  [(1, 4)]
  def apply_calculations(items):
      transformed = transform(items)
      for i, t in zip(items, transformed):
          yield calculate(i, t)

approach

Python 3.10 added a parameter called strict to thezip ()function. If you specify a positive value for strict, ValueError occurs when the lengths of the elements are not equal.

>>> for item in zip([1, 2, 3], [4], strict=True):
...     print(item)
...
(1, 4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: zip() argument 2 is shorter than argument 1

I'm getting an error that the second argument of zip () is shorter than the first argument. By the way, it should be noted that the loop runs until it detects that the lengths do not match.

By default, the behavior is the same as before (equivalent to strict = False, that is, if the lengths are different, the data is discarded).

>>> for item in zip([1, 2, 3], [4]):
...     print(item)
...
(1, 4)
>>>

Impressions

Recommended Posts

I read PEP 618 (Add Optional Length-Checking To zip)
I read PEP 584 (Add Union Operators To dict)
I read PEP 613 (Explicit Type Aliases)
I read PEP 612 (Parameter Specification Variables)
I read PEP 604 (Complementary syntax for Union []).
I read PEP-362 (Function Signature Object) Memo
What I always add to my ~ / .bashrc
I read PEP 614 (Relaxing Grammar Restrictions On Decorators)
I read "How to make a hacking lab"
I read PEP-593 (Flexible function and variable annotations)
I tried to read and save automatically with VOICEROID2 2
I read PEP-544 (Protocols: Structural subtyping (static duck typing)).
I read PEP 585 (Type Hinting Generics In Standard Collections)
I tried to automatically read and save with VOICEROID2
I read the Chainer reference (updated from time to time)