Python tricks

(New features, helpful idioms)

Dan Fitch

2021-11-17

3.9 and 3.10

Some of these features are very new, and depend on the new parser that came with 3.9.

Destructuring

Structural pattern matching

  • Like ML family languages!
  • Some examples
  • (and static typing is getting cleaner and more Ocaml-like)

Walrus operator

frozenset

zoneinfo

Actual proper IANA time zone support

Dictionary union operators

| or |= (in-place), works on most dictionary-like types (defaultdict, lists of 2-tuples, etc.)

removesuffix/prefix

  • .strip does weird, unexpected stuff
  • "new cool features in Python".strip(" in Python") -> "ew cool features"
  • removesuffix/prefix do exactly what you want
  • "new cool features in Python".removesuffix(" Python")

THE END

(Q&A, discussion)

HOORAY!!