Instead of using dict + dict, perhaps use dict.flow_update. Here,
flow_update is just like update, except that it returns self.
There's a difference between a sorted copy of a list, and sorting the
list in place.
>>> items = [2, 0, 1, 9]
>>> sorted(items), items
([0, 1, 2, 9], [2, 0, 1, 9])
>>> items.sort(), items
(None, [0, 1, 2, 9])
In Python, mutating methods generally return None. Here, this prevents
beginners thinking their code has produced a sorted copy of a list,
when in fact it has done an in-place sort on the list. If they write
>>> aaa = my_list.sort()
they'll get a None error when they use aaa.
The same goes for dict.update. This is a useful feature, particularly
for beginners. It helps them think clearly, and express themselves
This returning None can be a nuisance, sometimes. Suppose we have a
dictionary of default values, and a dictionary of use supplied
options. We wish to combine the two dictionaries, say into a new
One way to do this is:
combined = defaults.copy()
But this is awkward when you're in the middle of calling a function:
# lots of arguments, one to a line, with comments
arg = combined, # Look up to see what combined is.
# more arguments
There's a suggestion, that instead one extends Python so that this works:
arg = defaults + options # What does '+' mean here?
Here's another suggestion. Instead write:
dict_arg = defaults.copy().flow_update(options) # Is this clearer?
Here's an implementation, as a subclass of dict.
def flow_update(self, *argv, **kwargs):
A DIRTY HACK
Not tested, using an assignment expression.
dict_arg = (tmp := defaults.copy(), tmp.update(options))