
On Thu, Feb 12, 2015 at 4:56 PM, Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
There's an obvious way out of all this. We add *two* new operators:
d1 >> d2 # left operand wins d1 << d2 # right operand wins
And if we really want to do it properly:
d1 ^ d2 # raise exception on duplicate keys
+1 for having a standard solution to this problem. I have encountered it enough times to believe it is worth solving. If the solution is to be with operators, then +1 for << and >> as the least ambiguous. The only downside in my mind is that the need for combining dicts will be fairly rare, so there might not be enough justification to create new idioms for << and >>. Because it is a rare enough use case, though, a non-operator method might be the way to go, even though it is less algebraically pure. I like the idea of an operator or method that raises an exception on duplicate keys, but am not sure (a) whether such an exception should be raised if the two keys are mapped to equal values, or only if the two dicts are "in conflict" for some common key, and (b) whether, by analogy to set, ^ suggests that duplicate keys should be *omitted* from the result rather than triggering an exception. Nathan