On Mon, Jul 28, 2014 at 5:16 PM, Stephen J. Turnbull <stephen@xemacs.org> wrote:
Alexander Heger writes:
It seems it would be valuable to parallel the behaviour of operators already in place for collections.
Mappings aren't collections. In set theory, of course, they are represented as *appropriately restricted* collections, but the meaning of "+" as applied to mappings in mathematics varies. For functions on the same domain, there's usually an element-wise meaning that's applied. For functions on different domains, I've seen it used to mean "apply the appropriate function on the disjoint union of the domains".
I don't think there's an obvious winner in the competition among the various meanings.
The former meaning requires that the member types support addition, so it's the obvious loser -- dicts can contain any kind of value, not just addable ones. Adding a method that only works if the values satisfy certain extra optional constraints is rare in Python, and needs justification over the alternatives. The second suggestion works just fine, you just need to figure out what to do with the intersection since we won't have disjoint domains. The obvious suggestion is to pick an ordering, just like the update method does. For another angle: the algorithms course I took in university introduced dictionaries as sets where the members of the set are tagged with values. This makes set-like operators obvious in meaning, with the only question being, again, what to do with the tags during collisions. (FWIW, the meaning of + as applied to sets is generally union -- but Python's set type uses | instead, presumably for analogy with ints when they are treated as a set of small integers). That said, the only reason I can think of to support this new stuff is to stop dict(x, **y) from being such an attractive nuisance. -- Devin