3 ms·
>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
by 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.