This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add operation "plus" for dictionaries
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Pix, terry.reedy, vstinner
Priority: normal Keywords:

Created on 2014-06-06 10:17 by Pix, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg219867 - (view) Author: Михаил Мишакин (Pix) Date: 2014-06-06 10:17
First of all, i'm sorry for my English :)

I would like to union dictionaries with operator + (and +=) like this:

>>> dict(a=1, b=2) + {'a': 10, 'c': 30}
{'a': 10, 'b': 2, 'c': 30}

>>> d = dict(a=1, b=2, c={'c1': 3, 'c2': 4})
>>> d += dict(a=10, c={'c1':30})
>>> d
{'a': 10, 'b': 2, c: {'c1':30}}


Also, it gives an easy way to modify and extend the class attributes:

class Super:
   params = {
       'name': 'John',
       'surname': 'Doe',
   }

class Sub(Super):
   params = Super.params + {
       'surname': 'Show',
       'age': 32,
   }
msg219868 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-06-06 10:23
You should use dict.update() method.
msg219869 - (view) Author: Михаил Мишакин (Pix) Date: 2014-06-06 10:39
Is's like list's operation + and it's method list.extend().
But dict have no operation +...

If I have two lists (A and B), and I want to get third list (not change A and B) i do this:
C = A + B

If I have two dicts, i can do this:
C = dict(A, **B)

But if i have three dictionaries, code becomes this:
C = dict(A, **dict(B, **D))

Don't you think, that "+" is more comfortable?
A = [1, 2, 3]
B = [4, 5]
C = [6, 7]
A + B + C = [1,2,3,4,5,6,7]

I can do this with list, tuples and strings. Why i can't do this with dictionaries?
msg219907 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-06-07 01:55
This has been proposed, discussed on both python-list and python-ideas, and rejected more than once because there are there are multiple possible response to multiple keys: keep first value, keep second value (.update), keep both (in a list), or keep neither and raise.

'Obvious' ideas like this should be floated on one of those two lists to find out past response.
History
Date User Action Args
2022-04-11 14:58:04adminsetgithub: 65877
2014-06-07 01:55:48terry.reedysetstatus: open -> closed

type: enhancement
versions: + Python 3.5, - Python 3.4
nosy: + terry.reedy

messages: + msg219907
resolution: rejected
stage: resolved
2014-06-06 10:39:27Pixsetmessages: + msg219869
2014-06-06 10:23:35vstinnersetnosy: + vstinner
messages: + msg219868
2014-06-06 10:17:45Pixcreate