Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue6410] Dictionaries should support __add__

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

Jul 3, 2009, 1:33 PM

Post #1 of 17 (277 views)
Permalink
[issue6410] Dictionaries should support __add__

New submission from Michael W. <hotdog003[at]gmail.com>:

Summary:
Dictionaries should support being added to other dictionaries instead of
using update(). This should be a relatively easy fix and would make the
language more pythonic.

How to reproduce:
$ python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> {1: 2, 3: 4} + {5: 6, 7: 8}

What happens:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'

What should happen:
{1: 2, 3: 4, 5: 6, 7: 8}

Temporary workaround:
>>> a = {1: 2, 3: 4}
>>> b = {5: 6, 7: 8}
>>> c = a.copy()
>>> c.update(b)
>>> print c
{1: 2, 3: 4, 5: 6, 7: 8}
This is undesirable because it is not very compact and hard to read. Why
should any language take five lines of code to merge only two dictionaries?

----------
components: Interpreter Core
messages: 90074
nosy: hotdog003
severity: normal
status: open
title: Dictionaries should support __add__
type: feature request
versions: Python 2.6

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue6410>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jul 3, 2009, 2:14 PM

Post #2 of 17 (267 views)
Permalink
[issue6410] Dictionaries should support __add__ [In reply to]

Martin v. Löwis <martin[at]v.loewis.de> added the comment:

What would you do in case of duplicate keys?

----------
nosy: +loewis

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue6410>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jul 3, 2009, 2:37 PM

Post #3 of 17 (265 views)
Permalink
[issue6410] Dictionaries should support __add__ [In reply to]

Alexandre Vassalotti <alexandre[at]peadrop.com> added the comment:

I am against adding __add__ to dict, since merging dictionaries is not a
commutative operation.

If a short syntax is desired for merging dictionaries, the just define a
function. For example:

def merge_dicts(*args):
result = {}
for x in args:
result.update(x)
return result

----------
nosy: +alexandre.vassalotti
resolution: -> rejected
status: open -> pending

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue6410>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jul 4, 2009, 3:34 PM

Post #4 of 17 (255 views)
Permalink
[issue6410] Dictionaries should support __add__ [In reply to]

Tim Gordon <tim.py[at]aleph17.co.uk> added the comment:

__add__ is non-commutative for lists, tuples, strings etc. - perhaps
non-commutative wasn't quite what you were looking for :p.

----------
nosy: +QuantumTim
status: pending -> open

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue6410>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jul 4, 2009, 3:39 PM

Post #5 of 17 (254 views)
Permalink
[issue6410] Dictionaries should support __add__ [In reply to]

Jean-Paul Calderone <exarkun[at]divmod.com> added the comment:

Why so much opposition to the shorter spelling of .copy() & .update()?
As Tim pointed out, lists, tuples, and strings all provide this
shortcut. Why not dicts?

----------
nosy: +exarkun

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue6410>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jul 4, 2009, 4:27 PM

Post #6 of 17 (255 views)
Permalink
[issue6410] Dictionaries should support __add__ [In reply to]

Benjamin Peterson <benjamin[at]python.org> added the comment:

Lists, tuples, and strings are all sequences. Adding two non-ordering
mappings makes much less sense in my head than two sequences.

----------
nosy: +benjamin.peterson
status: open -> closed

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue6410>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jul 4, 2009, 4:37 PM

Post #7 of 17 (253 views)
Permalink
[issue6410] Dictionaries should support __add__ [In reply to]

Alexandre Vassalotti <alexandre[at]peadrop.com> added the comment:

Tim Gordon wrote:
> __add__ is non-commutative for lists, tuples, strings etc. - perhaps
> non-commutative wasn't quite what you were looking for :p.

Yeah, I was not clear in my explanation.

The thing is for lists, tuples, string and other ordered types
concatenation is a well-defined concept. Whereas for dictionaries is not
obvious what concatenation should do with duplicate keys. For example,
what would be the result of {"a": 1, "b": 2} + {"a": 2, "b": 1}? Should
it be {"a": 1, "b": 2} or {"a": 2, "b": 1}?

Also, it would be inconsistent the use of | for the union operation of sets.

----------
status: closed -> open

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue6410>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jul 4, 2009, 4:38 PM

Post #8 of 17 (254 views)
Permalink
[issue6410] Dictionaries should support __add__ [In reply to]

Changes by Alexandre Vassalotti <alexandre[at]peadrop.com>:


----------
status: open -> closed

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue6410>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jul 4, 2009, 4:51 PM

Post #9 of 17 (253 views)
Permalink
[issue6410] Dictionaries should support __add__ [In reply to]

Ezio Melotti <ezio.melotti[at]gmail.com> added the comment:

A dict.merge() method could be added so that:

>>> a = dict(x=10, y=20)
>>> b = dict(y=30, z=40)
>>> a.merge(b)
dict(x=10, y=20, z=40)
>>> b.merge(a)
dict(y=30, z=40, x=10)

In case of duplicate keys, the items of the second dict with the same
keys will be discarded (even if dict.update() does the opposite, I think
here it make more sense in this way).

I never needed to do something like this though, so I'm +0 about it.

----------
nosy: +ezio.melotti
priority: -> low
resolution: rejected ->
status: closed -> open
versions: +Python 2.7, Python 3.2 -Python 2.6

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue6410>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jul 4, 2009, 5:23 PM

Post #10 of 17 (255 views)
Permalink
[issue6410] Dictionaries should support __add__ [In reply to]

Raymond Hettinger <rhettinger[at]users.sourceforge.net> added the comment:

IIRC, Guido has previously rejected this suggestion and its variants.

----------
nosy: +rhettinger

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue6410>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jul 4, 2009, 8:26 PM

Post #11 of 17 (233 views)
Permalink
[issue6410] Dictionaries should support __add__ [In reply to]

Jean-Paul Calderone <exarkun[at]divmod.com> added the comment:

> IIRC, Guido has previously rejected this suggestion and its variants.

Got a link? It'd be nice to know what the rationale was.

----------

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue6410>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jul 4, 2009, 8:41 PM

Post #12 of 17 (233 views)
Permalink
[issue6410] Dictionaries should support __add__ [In reply to]

Benjamin Peterson <benjamin[at]python.org> added the comment:

Regardless of whether this feature will be dict.merge or __add__, it
needs to work its way through python-ideas first.

----------
resolution: -> rejected
status: open -> closed

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue6410>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jul 6, 2009, 2:23 PM

Post #13 of 17 (214 views)
Permalink
[issue6410] Dictionaries should support __add__ [In reply to]

Raymond Hettinger <rhettinger[at]users.sourceforge.net> added the comment:

FWIW, I'm -1 on the proposal because it partially overlaps the existing
capability of dict.update(). To the extent it doesn't overlap, it is
use case challenged (typically, it doesn't make sense to build a
brand-new dictionary from two independent dictionaries and the atypical
case easily fulfilled by a couple of updates on an empty dict).

Also, the notation itself is at odds with the existing pipe-operator
used by sets and by dict views.

FWIW, there are other alternatives to directly combining dictionaries.
See http://code.activestate.com/recipes/305268/ for one example.

----------

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue6410>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jul 6, 2009, 2:47 PM

Post #14 of 17 (214 views)
Permalink
[issue6410] Dictionaries should support __add__ [In reply to]

Jean-Paul Calderone <exarkun[at]divmod.com> added the comment:

FWIW, here are some use cases:

http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.2.0/twisted/python/context.py#L32
http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.2.0/twisted/python/log.py#L270
http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.2.0/twisted/words/xish/utility.py#L23
http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.2.0/twisted/web/microdom.py#L720
http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.2.0/twisted/web/microdom.py#L738
http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.2.0/twisted/test/test_adbapi.py#L370
http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.2.0/twisted/test/test_zshcomp.py#L34
http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.2.0/twisted/mail/scripts/mailmail.py#L345
http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.2.0/twisted/internet/_dumbwin32proc.py#L167
http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.2.0/twisted/lore/default.py#L17

----------

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue6410>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jul 6, 2009, 4:59 PM

Post #15 of 17 (213 views)
Permalink
[issue6410] Dictionaries should support __add__ [In reply to]

Raymond Hettinger <rhettinger[at]users.sourceforge.net> added the comment:

ISTM these examples show how little value would come from fattening-up
the dict API. The examples use the copy/update pattern which is clear,
explicit, and extendable to n-ary cases without incurring O(n**2)
behavior. Tranforming them to a f=d+e pattern saves a few characters;
doesn't add any speed; makes it less clear that we're operating on
dictionaries; and does not extend well to the n-ary case (f=a+b+c+d+e
which copies a's elements five times, b's four times, etc.) No new
functionality gets added -- all this is is a piece of syntactic sugar
that obscures what is going-on under the hood. The dict API is one of
the most fundamental in the language; it needs to be kept as clean and
hazard-free as possible.

----------

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue6410>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jul 6, 2009, 5:00 PM

Post #16 of 17 (214 views)
Permalink
[issue6410] Dictionaries should support __add__ [In reply to]

Jean-Paul Calderone <exarkun[at]divmod.com> added the comment:

Cool. I'm convinced.

----------

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue6410>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jul 10, 2009, 2:01 PM

Post #17 of 17 (206 views)
Permalink
[issue6410] Dictionaries should support __add__ [In reply to]

Terry J. Reedy <tjreedy[at]udel.edu> added the comment:

> what would be the result of {"a": 1, "b": 2} + {"a": 2, "b": 1}?
> Should it be {"a": 1, "b": 2} or {"a": 2, "b": 1}?

or {"a":[1,2], "b":[1,2]}
As I remember, Guido rejected because of this ambiguity.

----------
nosy: +tjreedy

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue6410>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.