(original) (raw)

I split off a separate thread on python-ideas \[1\] specific to the idea of introducing "+" and "+=" operators on a dict.

\[1\] https://mail.python.org/pipermail/python-ideas/2015-February/031748.html


\~ Ian Lee

On Tue, Feb 10, 2015 at 10:35 PM, John Wong <gokoproject@gmail.com> wrote:


On Wed, Feb 11, 2015 at 12:35 AM, Ian Lee <ianlee1521@gmail.com> wrote:
+1 for adding "+" or "|" operator for merging dicts. To me this operation:

>>> {'x': 1, 'y': 2} + {'z': 3}
{'x': 1, 'y': 2, 'z': 3}

Is very clear. The only potentially non obvious case I can see then is when there are duplicate keys, in which case the syntax could just be defined that last setter wins, e.g.:

>>> {'x': 1, 'y': 2} + {'x': 3}
{'x': 3, 'y': 2}

Which is analogous to the example:

new\_dict = dict1.copy()
new\_dict.update(dict2)


Well looking at just list
a + b yields new list
a += b yields modified a
then there is also .extend in list. etc.

so do we want to follow list's footstep? I like + because + is more natural to read. Maybe this needs to be a separate thread. I am actually amazed to remember dict + dict is not possible... there must be a reason (performance??) for this...