[Python-Dev] PySequence_Concat for dicts
Jared Flatow
jflatow at northwestern.edu
Sat Jan 12 17:51:28 CET 2008
> On Jan 11, 2008 5:21 PM, Raymond Hettinger <python at rcn.com> wrote:
>> When does it come-up that you want a third summed dict
>> while keeping the two originals around unchanged? Does
>> it matter that the addition is non-commutative? Would
>> a + b + c produce an intermediate a/b combo and then
>> another new object for a/b/c so that the entries in
>> a get copied twice and memory usage has to hold a, b,
>> a/b, c, and a/b/c in memory all at the same time?
There is no way around it, this will be less efficient than the
inplace operation. If there were a precedent for calling a method
like update and returning itself instead of None, I would suggest
that, but since this is the way that has already been established for
lists, it seems a natural extension.
On Jan 11, 2008, at 9:22 PM, Guido van Rossum wrote:
> It does suggest that we have two choices for the proposed operation:
> d1+d2 or d1|d2. I think the latter may be more appropriate:
> len(seq1+seq2) == len(seq1) ++len(seq2), but no such invariant exists
> for set union.
This might be way out there but I suppose you could consider that a
dict is actually two different things, depending on what you are
doing, and that these operations might actually do different things.
Interpreted as a sequence, it is a sequence of key mappings. As
confirmed in another recent discussion, Python guarantees consistent
(read: repeatable) ordering of iteration through a dict, so in this
sense it really is a sequence. (On a side note, I frequently rely on
the repeatability of ordering when interacting with the Python shell).
The notable sequence operation being + for concatenation, would
perform an update of the keys (thus for the sequence op the mappings
aren't guaranteed to be preserved, only the keys).
The other interpretation of dict is obviously as a set of (key,
value) pairs. For sets, the four major operations could behave
exactly as any other set of (key, value) tuples (i.e. if you
transform it to a list and then apply the same ops you should get the
same result).
jared
p.s. If I were to get approval to implement some version of this,
which version of Python would be appropriate to work with?
More information about the Python-Dev
mailing list