Technical Books
My notes & review of Py Donts by Rodrigo Girão Serrão
Author

Tyler Hillery

Published

February 11, 2025


Notes

Zip Up

  • zip goes until the shortest of the iterators provided
  • In Python 3.10> you can use a strict keyword to error if the length of iterators don’t match
firsts = ["Tyler", "Chandra"]
lasts = ["Hillery", "Hillery", "Hill"]

try:
    for z in zip(firsts, lasts, strict=True):
        print(z)
except Exception as e:
    print(e)
('Tyler', 'Hillery')
('Chandra', 'Hillery')
zip() argument 2 is longer than argument 1
  • The error doesn’t occur until the end when the mismatch occurs because zip is lazily evaluated
  • Interesting, you can use zip inside dict to make key,value pair of the iterators passed in

Chaining comparison operators

  • Pitfall to watch out for is chaining != together.
a = c = 1
b = 2
if a != b != c:
    print("a, b, and c are all different: ", a, b, c)
a, b, and c are all different:  1 2 1
  • a != b != c really evaluates to a != b and b != c it doesn’t tell you anything about a relates to c. Stick with chaining comparison operators on transitive operations

Boolean short-circuiting