It seems reasonable to believe that dict.pop
operates atomically, since it raises KeyError
if the specified key is missing and no default is provided, like so:
d.pop(k)
However, the documentation does not appear to specifically address that point, at least not in the section specifically documenting dict.pop
.
This question occurred to me as I was reviewing an answer of mine which used this pattern:
if k in d: del d[k]
At the time, I was not thinking of the potential condition that a key may be present during the if
, but not at the time of del
. If dict.pop
does indeed provide an atomic alternative, then I should note that in my answer.