Why doesn’t a python dict.update() return the object?

Python’s mostly implementing a pragmatically tinged flavor of command-query separation: mutators return None (with pragmatically induced exceptions such as pop😉 so they can’t possibly be confused with accessors (and in the same vein, assignment is not an expression, the statement-expression separation is there, and so forth).

That doesn’t mean there aren’t a lot of ways to merge things up when you really want, e.g., dict(a, **award_dict) makes a new dict much like the one you appear to wish .update returned — so why not use THAT if you really feel it’s important?

Edit: btw, no need, in your specific case, to create a along the way, either:

dict(name=name, description=desc % count, points=points, parent_award=parent,
     **award_dict)

creates a single dict with exactly the same semantics as your a.update(award_dict) (including, in case of conflicts, the fact that entries in award_dict override those you’re giving explicitly; to get the other semantics, i.e., to have explicit entries “winning” such conflicts, pass award_dict as the sole positional arg, before the keyword ones, and bereft of the ** form — dict(award_dict, name=name etc etc).

Leave a Comment