set operations on dictionaries?

2006-10-16 Thread aba cus
A suggestion:In effect, sets are dictionaries without values. (Even thought their implementation is different)My suggestion would be to generalise set operations (such as union, intersection, etc.) to work for dictionaries just as they work on sets.>>> Set([1,2,3]) - Set([3,4])Set([1, 2])>>> {1: 10, 2: 20, 3: 30} - {3: 30, 4: 40}should give{1: 10, 2:20}instead of"TypeError: unsupported operand type(s) for -: 'dict' and 'dict'"Similarly, union ( | ) and intersetion ( & ) should also be very useful for dictionaries.It should be quite obvious what should happen when the values stored for the relevant keys are identical. It is less obvious (and needs some though) what should  happen when the keys are the same in the
 two operands,  but the values are different.E.g. Should{1: 10, 2: 20} | {2: 25, 3:30}give an error message (because the values for the key 2 don't match), or should this value conflict be somehow resolved automatically?Similarly, should intersection and differenc eliminate keys only when the keys match, or not.I think this would be useful. Would this be worth implementing? Would it be difficult to implement?-- 
http://mail.python.org/mailman/listinfo/python-list

Re: set operations on dictionaries?

2006-10-16 Thread aba cus
              It should be quite obvious what should happen when the values stored forthe relevant keys are identical. It is less obvious (and needs somethough) what should happen when the keys are the same in the twooperands, but the values are different.I think you answered you own proposal in the negative.  Precisely becausethe result of this example{1: 10, 2: 20} | {2: 25, 3:30}is ill-defined, Python shouldn't implement set operations
 on dictionaries."In the face of ambiguity, refuse the temptation to guess."I understand this point but this is just an argument for saying that it should throw an exception when the values don't match. It is not an argument for not doing the logical thing when they do. In fact in many situations it can be reasonably expected that the values will be the same. If not, give an error. There is no danger in that. The programmer should know whether this is a reasonable expectation in any givn case.(As it is at the moment, you get an error even if they match...)-- 
http://mail.python.org/mailman/listinfo/python-list