←back to thread

133 points _ZeD_ | 1 comments | | HN request time: 0.204s | source
Show context
zelphirkalt ◴[] No.44538027[source]
I found dictionary unpacking to be quite useful, when you don't want to mutate things. Code like:

    new_dict = {**old_dict, **update_keys_and_values_dict}
Or even complexer:

    new_dict = {
        **old_dict,
        **{
            key: val
            for key, val in update_keys_and_values_dict
            if key not in some_other_dict
        }
    }
It is quite flexible.
replies(1): >>44538627 #
peter422 ◴[] No.44538627[source]
I love the union syntax in 3.9+:

  new_dict = old_dict | update_keys_and_values_dict
replies(1): >>44538710 #
parpfish ◴[] No.44538710[source]
Don’t forget the in place variant!

  the_dict |= update_keys_and_values_dict
replies(1): >>44539624 #
masklinn ◴[] No.44539624[source]
No no, do forget about it: like += for lists, |= mutates “the dict”, which often makes for awkward bugs.

And like += over list.extend, |= over dict.update is very little gain, and restricts legal locations (augmented assignments are statements, method calls are expressions even if they return "nothing")

replies(1): >>44541813 #
IgorPartola ◴[] No.44541813[source]
The |= does exactly what it says on the tin. How could it not mutate the left side of the assignment?
replies(2): >>44542549 #>>44542850 #
parpfish ◴[] No.44542549[source]
In typed languages, I’m all about using nice safe immutable variables/values.

But in python, everything is mutable so there’s only so much safety you can wring out of adhering the an immutable style. Any other function can hop in and start mutating things (even your “private” attributes). Plan for mutations occurring everywhere.

replies(1): >>44543051 #
1. graemep ◴[] No.44543051[source]
I find the fewer mutations the easier code is to understand, at the level of an individual function.

Of course you do not have the safety you would have in a language that enforces immutability, but there is still a cost (in terms of maintenance and the likelihood of bugs) to mutation.