3 ms·
len(dict1 + dict2) does not equal len(dict1) + len(dict2) so using the + operator is nonsense. The operators should be |, &, and -, exactly as for sets, and th
by zestyping 8y ago
len(dict1 + dict2) does not equal len(dict1) + len(dict2) so using the + operator is nonsense.
The operators should be |, &, and -, exactly as for sets, and the behaviour defined with just three rules:
1. The keys of dict1 [op] dict2 are the elements of dict1.keys() [op] dict2.keys().
2. The values of dict2 overwrite the values of dict1.
3. When either operand is a set, it is treated as a dict whose values are None.
This yields many useful operations and is simple to explain.
merge and update:
{'a': 1, 'b': 2} | {'b': 3, 'c': 4} => {'a': 1, 'b': 3, 'c': 4}
pick some items:
{'a': 1, 'b': 2} & {'b': 3, 'c': 4} => {'b': 3}
remove some items:
{'a': 1, 'b': 2} - {'b': 3, 'c': 4} => {'a': 1}
reset values of some keys:
{'a': 1, 'b': 2} | {'b', 'c'} => {'a': 1, 'b': None, 'c': None}
ensure all keys are present:
{'b', 'c'} | {'a': 1, 'b': 2} => {'a': 1, 'b': 2, 'c': None}
pick some items:
{'b', 'c'} | {'a': 1, 'b': 2} => {'b': 2}
remove some items:
{'a': 1, 'b': 2} - {'b', 'c'} => {'a': 1}
- petters 8y agoSo much better! Overloading addition for something that behaves differently is not good.
- rat9988 8y agoAddition as defined in mathematics behaves differently depending on context.
- antt 8y ago>The operators should be |, &, and -, exactly as for sets, and the behaviour defined with just three rules: len(a) - len(b) != len(a-b) either. I'm not really sure why you think that length should be a linear map for dictionaries. Their length is their least interesting property.
- zestyping 8y agoPython uses different operators for combining lists and for combining sets. So, the question is: is this operation more like list + or more like set |? Choosing len(a) + len(b) == len(a + b) as a criterion is interesting because it is what distinguishes an addition from a merge. Addition: put together two collections and keep everything Merge: put together two collections and keep some things This is a merge. The dictionary merge operator should be | for the same reason that the set merge operator is |.
- antt 8y agoYes again, for two sets: a = {1,2,3} b = {1,2,3,6,7,8} len(a) - len(b) == 0 len(a) != len(b) Subtraction on sets does not work like you're suggesting it should. I don't understand why you're using that to defend broken notation. The only reason why we have a|b as union is because a∪b is too hard to type on most keyboards, and we still are trying to catch up to APL when it comes to math source code.
- zestyping 8y agoI'm not suggesting anything about subtraction on sets. I'm suggesting that + is not appropriate for dictionary merge because it doesn't behave like + on other Python containers. The context is Python, not other languages. In a language that used + for set union, + would also be fine for dictionary merge. But Python isn't like that. Strings, tuples, and lists have + and do not have -, and for all of them the + operator does add lengths. Sets have | and -, and those operators do not add or subtract lengths.
- rbanffy 8y ago> {'a': 1, 'b': 2} - {'b': 3, 'c': 4} => {'a': 1} I think that removing the key from d1 would be a bad idea if the value is not the same on both dicts. If you think the dict is a vector of named dimensions, should 'c' be -4 in the result? I'd totally support it resulting in: {'a': 1, 'b': -1, 'c': -4}
- gpderetta 8y agoThe length (i.e magnitude) of the sum of two algebraic vectors is also not the length of the two original vectors. Would you not use + to represent vector sum?
- mwkaufma 8y agoWhy should that constraint hold? It's not even true for simple vectors under the euclidean norm: ||<1,0>|| + ||<0,1>|| != ||<1,0> + <0,1>||