What is it? The link points to a discussion more deep than I’m willing to read.
replies(10):
def foo():
if n := randint(0, 3):
return n ** 2
return 1337
[(x, y, x/y) for x in input_data if (y := f(x)) > 0]
if foo := bar[baz]:
bar[baz] += 1
return foo
else:
bar[baz] = 1
return 0
Where foo is a dict keeping track of multiple things, and a non-existing key (baz) is never an error but rather the start of a new count. Faster and more readable than if baz in list(bar.keys()):
....
Similar to Swift’s ‘if let’, it seems. try:
bar[baz] += 1
except KeyError:
bar[baz] = 1
Also you can check if a key is in a dict simply by doing "if baz in bar" no need for "list(bar.keys())", which will be slow (temp object + linear scan) vs O(1) hashmap lookup.