Python dictionary "addition" and "subtraction"
Python dictionary "addition" and "subtraction"
Posted Mar 13, 2019 21:35 UTC (Wed) by NYKevin (subscriber, #129325)In reply to: Python dictionary "addition" and "subtraction" by iabervon
Parent article: Python dictionary "addition" and "subtraction"
1. Single pipe is traditionally commutative (bitwise OR, set union, etc.). It has been used non-commutatively (shell pipelines), but that usage is so far afield that it provides no intuition here.
2. Double pipe (or the "or" keyword) traditionally short-circuits to the left. I've never seen it short-circuit to the right.
3. Plus is sometimes commutative, but its noncommutative uses traditionally preserve the order of items (concatenation) rather than allowing one item to "override" another. So the closest analogue here would be c = ChainMap(a, b) (so that you have c.maps == [a, b]) as Hettinger suggests... but that actually gives precedence to the left! ChainMap.new_child() does give precedence to the "right" in a sense, but it's type-asymmetric (self is a ChainMap, the argument usually isn't), and probably should not be an operator at all.
Posted Mar 13, 2019 22:11 UTC (Wed)
by iabervon (subscriber, #722)
[Link] (6 responses)
I'd sort of guess that, if you've got an "or"-like thing, even if it doesn't short-circuit (i.e., evaluates all of its arguments), it'll pick between distinguishable values with the same truth value as if it were short-circuiting.
Posted Mar 14, 2019 15:41 UTC (Thu)
by nybble41 (subscriber, #55106)
[Link] (5 responses)
>>> { 'apple': 5, 'pear': 7 } + { 'strawberry': 2, 'pear': 10 }
This would make it similar to the Semigroup operator (<>) in Haskell.
Posted Mar 14, 2019 17:17 UTC (Thu)
by NYKevin (subscriber, #129325)
[Link] (4 responses)
Posted Mar 15, 2019 11:29 UTC (Fri)
by jani (subscriber, #74547)
[Link] (3 responses)
Posted Mar 15, 2019 23:56 UTC (Fri)
by quietbritishjim (subscriber, #114117)
[Link] (2 responses)
[1] https://docs.python.org/3/library/collections.html#collec...
Posted Mar 17, 2019 11:35 UTC (Sun)
by jani (subscriber, #74547)
[Link] (1 responses)
Posted Mar 20, 2019 20:48 UTC (Wed)
by quietbritishjim (subscriber, #114117)
[Link]
Hmm, it turns out it doesn't work. The obvious thing is to use
This fails for two reasons:
An alternative is to use the
But this doesn't work either:
I think this is a bug in the documentation, which seems to say that these should be possible (at least for update()), or even a straight up bug in the code. But in fairness it is an unusual use of the class.
Python dictionary "addition" and "subtraction"
Python dictionary "addition" and "subtraction"
{ 'apple': 5, 'pear': 17, 'strawberry': 2 }
>>> { 'x': 'abc', 'z': { 'a': 13 } } + { 'x': 'def', 'y': 'ijk', 'z': { 'a': 9, 'b': 10 } }
{ 'x': 'abcdef', 'y': 'ijk', 'z': { 'a': 22, 'b': 10 } }
Python dictionary "addition" and "subtraction"
Python dictionary "addition" and "subtraction"
Python dictionary "addition" and "subtraction"
Python dictionary "addition" and "subtraction"
Python dictionary "addition" and "subtraction"
+:
Counter({'a': [1], 'b': [1, 2]}) + Counter({'a': [2, 3], 'b': [3]})
<0 for each element, which fails for lists.update() method, which doesn't have a restriction to positive values so doesn't compare against zero:
c = Counter({'a': [1], 'b': [1, 2]})
c.update(Counter({'a': [2, 3], 'b': [3]}))
c['a'] == [2,3,1]! This is true in Python 3.7 but not Python 2.7.
