Python Walrus operator
Good examples of the Python walrus operator.
TODO: check out presentation from PyCon 2019 introducing it.
From: https://www.reddit.com/r/Python/comments/1rw18i5/comment/oaxdl2e/
# perfect for re.match
if has_whatever := re.match(stuff)
a, b = has_whatever.groups()
###
only call other_function if res is some value
if (result := some_fun()) in ("a", "b"):
other_function(res)
###
# conditional based on the result of res
if func(res := gfunc()):
# use res safely
###
while line := fp.readline():
...
###
if bad := [r for r in records if r.bad()]:
raise ValueError('Bad records: ' + ', '.join(b.name for b in bad))