#WalrusOperator

2025-05-02

Encapsulating the atrocity of umask handling with Python's walrus operator:

os.umask(current_umask := os.umask(0))

This isn't clearly *better*, but at least it's clearly shorter. Without `:=`, we'd need two lines:

current_umask = os.umask(0)
os.umask(current_umask)

Note: The awkwardness is a necessary consequence of how POSIX handles umask. Also, neither version is threadsafe.

#Python #WalrusOperator #umask

Alexandre B A Villares 🐍villares@ciberlandia.pt
2024-09-16

would you use the #WalrusOperator?

 if test_fit(i, j, s) or test_fit(i, j, s := 1):
fill_grid(i, j, s)

or make it a bit longer like this

if test_fit(i, j, s):
fill_grid(i, j, s)
elif test_fit(i, j, 1):
fill_grid(i, j, 1)

? #Python

2023-02-24

one thing I would love for python is to be able to say
if not s := some_query(args):
stuff if the query fails
else:
stuff if the query succeeds and you have s assigned

#python #walrus #walrusoperator

Russell Keith-Mageefreakboy3742
2019-04-24

OMGWANT
---
RT @emilyemorehouse@twitter.com
They’re heeeeeeeere!
twitter.com/emilyemorehouse/st

Client Info

Server: https://mastodon.social
Version: 2025.04
Repository: https://github.com/cyevgeniy/lmst