[Python-Dev] PySequence_Concat for dicts (original) (raw)
Guido van Rossum guido at python.org
Sat Jan 12 18:32:29 CET 2008
- Previous message: [Python-Dev] PySequence_Concat for dicts
- Next message: [Python-Dev] PySequence_Concat for dicts
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Jan 12, 2008 8:51 AM, Jared Flatow <jflatow at northwestern.edu> wrote:
> 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).
No, a dict is not a set of (key, value) pairs. The keys form a set, you cannot have duplicate keys.
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?
Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
- Previous message: [Python-Dev] PySequence_Concat for dicts
- Next message: [Python-Dev] PySequence_Concat for dicts
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]