Mastodon Feed: Post

Mastodon Feed

Boosted by glyph ("Glyph"):
rckenned@hachyderm.io ("Ryan Kennedy") wrote:

Dear #Python folks, what’s the most Python-ic way to insert into a dict such that overwrites cause an error?

There are ways to silently overwrite (d[key] = value, update(…), etc) and ways to silently drop overwrites (setdefault). But the only way to detect and hard reject an overwrite seems to be to pre-check if the key is present.

Maybe it’s my 25+ years of Java instincts, but the part of my brain that’s been traumatized by race conditions wants something more resilient. Do I just embrace the GIL here and let go of my instincts? My Java instincts want something like java.util.ConcurrentMap’s putIfAbsent(key, value) method.