loading...

re: Python: Merging and Updating Dicts the New Way VIEW POST

FULL DISCUSSION
 

That's sweet syntactic sugar, sprinkled on top of an already generously topped doughnut (or something like that)...

I think that these two new operators (to me at least) looks very "visually" logical.
It's kinda like using "pipes" in *nix commands, as in:

# dict3 is the result of dict1 being 'piped' through dict2, resulting in a union of these.
dict3 = dict1 | dict2

That is awesome.

Maybe even more awesome, the augmented assignment version, |= operates in-place.
Where one would have resorted to using the awkward update() method and create a temporary as well, something that undoubtedly felt rather clunky, in 3.9 this is one expression, which updates dict1 in-place, no need for any temporary throw-away object.

The question that popped up first was "Hmm, but what about the cases where you get overlapping keys as a result? This would obviously crash the party.

But, thankfully, key conflicts will be resolved by keeping the rightmost value.
This matches the existing behavior of similar dict operations and keeps things consistent and clean.

Way to go Python!

code of conduct - report abuse