Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

JavaScript deals with this well. The equivalent of the proposed

    d3 = d1 + d2
is

    let d3 = {...d1, ...d2};
In fact, it seems like you can already do this in python:

    d3 = {**d1, **d2}
This seems to have most of the benefits of being a dedicated syntax (rather than just a function call), without the downsides of breaking the commutativity of the + operator.


From https://www.python.org/dev/peps/pep-0584/#current-alternativ...

> To create a new dict containing the merged items of two (or more) dicts, one can currently write:

  {**d1, **d2}
> but this is neither obvious nor easily discoverable. It is only guaranteed to work if the keys are all strings. If the keys are not strings, it currently works in CPython, but it may not work with other implementations, or future versions of CPython.

> It is also limited to returning a built-in dict, not a subclass, unless re-written as MyDict(d1, d2), in which case non-string keys will raise TypeError.


Requires Python3


Surely any new syntax (like +) would also require python3, so that's a bit of a moot point...


Yeah, but I didn't know it was new syntax. And presumably some others didn't either.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: