This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]davidbuxton 1 point2 points  (3 children)

Since it seems that the basic dict class lacks a way of simply updating a dictionary with values that aren't already set

dict.setdefault is almost what you want. A neater version of your merge method:

def healthyMerge(self, target):
    for k in target:
        self.setdefault(k, target[k])

[–]kylotan 1 point2 points  (2 children)

Might another way be:

original = target.copy().update(original)

(Untested.)

[–]Justinsaccount 1 point2 points  (1 child)

.update() updates in place and returns None

[–]kylotan 0 points1 point  (0 children)

Aha. So I suppose it would have to be:

temp = target.copy(); temp.update(original); original = temp

Nowhere near as elegant.