[Python-ideas] [Python-Dev] hello, new dict addition for new eve ?
nathan at cmu.edu
Sat Dec 31 23:57:02 CET 2011
On Sat, Dec 31, 2011 at 4:55 PM, Terry Reedy <tjreedy at udel.edu> wrote:
> On 12/31/2011 11:07 AM, julien tayon wrote:
>> The question is a dict the same as a vector or as a set ?
> A dict is a mapping, which is to say, a functional set of key,value pairs.
> 'Functional' means that the keys are unique. This is a key property (pun
> intended). A vector can be regarded as a functional set of count,value
> pairs. Some languages use dicts for vectors. Even in Python, people use
> dicts for sparse arrays of various dimensions, with unspecified pairs
> assumed to have a 0 value.
> So it makes the most sense to me for d1 + d2 to be interpreted as set union,
> with some resolution of key conflicts to restore the functional property.
> Your vector interpretation of adding values only works for values that can
> be added.
It occurs to me that any argument for a vector interpretation of
dicts, with element-wise operations, should apply equally well to
lists and tuples, which are perhaps even more natural as
representations for vectors. But of course, the + operator for
sequences is interpreted as concatenation, not element-wise addition;
it would be strange if the reverse were true for dicts.
The necessary resolution of key conflicts is what makes "+" feel less
natural to me for dicts than it does for sequences, where the order of
the operands is transparently reflected in the output. For dicts, in
contrast, we are talking about a "lossy" union/combination where what
is lost depends on the order of the operands—this is less transparent
in the output. However it is spelled, though, I would be thrilled if
such an operation were built in. :)
>> since sets are using logical operators for operations that are roughly
>> sets operations why not use & ^ | for sets operation on dict ?
>> it would be pretty consistent with sets operations.
> I agree, except that dicts are not just sets of pairs, but functional sets.
> If one wants to view a dict simply as a set of keys, values, or pairs,
> ignoring the functional property for the pairs, that is what the .keys(),
> .values(), and .items() set view methods are for. All the set operations are
> available on the resulting set view objects.
> Terry Jan Reedy
> Python-ideas mailing list
> Python-ideas at python.org
More information about the Python-ideas