Live data from Hacker News

Dictionary union (PEP 584) is merged

github.com

21–30 of 87 posts

Re: Dictionary union (PEP 584) is merged

#25
post #5

Earlier quoted context omitted.

Guido himself said he had forgotten about this trick and since it's syntactic sugar, it does not respect dict subclasses or other mappings.

imo defaultdict(callback,{**a,**b}) is more readable than a | b without knowing that a or b are defaultdicts and having to reason about which default callback will be used

Note that a | b is already a disaster if you try to use subclasses of set. In python 2.7, iirc it would return a set of a's type but without calling the constructor. In python 3 it seems to return a set (not the type of a or the type of b).

Re: Dictionary union (PEP 584) is merged

#26
post #24
post #8

{**d1, **d2} is very natural if you also write javascript where their spread operator looks like: {...d1, ...d2}

So PEP 584 is a JavaScript inspired feature request?

No, it adds a new merge (|) and update (|=) operator to avoid using

    {**d1, **d2} 
because it apparently looks ugly[1].

[1]: https://www.python.org/dev/peps/pep-0584/#d1-d2

Re: Dictionary union (PEP 584) is merged

#29
post #13
post #11

Earlier quoted context omitted.

It mentions in the PEP discussion that the { a, b} trick only works for string keys. So it isn't applicable in as many cases as the new operator.

actually, only dict(d1, **d2) has that problem. it works fine if you unpack into a dict literal: >>> d1 = {1: 'a'} >>> d2 = {2: 'b'} >>> {**d1, **d2} {1: 'a', 2: 'b'} iirc the pep mostly just says that it's suboptimal because it's syntactically heavy/noisy, non-obvious and can't be overloaded in dict subclasses --- i was curious why the two double-stars behave differently despite syntactic similarity. so i went and c…

This seems pretty straightforward. When doing

`dict(d1, d2)`

You are calling the dict function, and using the normal syntax for unpacking a dictionary into kwargs. In this case, the name for kwargs must be strings.

Re: Dictionary union (PEP 584) is merged

#30
post #16

Fantastic. Especially glad they went with | over +, that’s always felt like the natural way I’ve wanted to do this. Looking forward to more set-like operators in the future!

Thank goodness sanity prevailed on the operator!

We had a whole discussion on HN last time[1] about this, where I argued that dicts are logically subclasses of sets and therefore should share operators.

When I saw this headline I accepted my fate of typing the "wrong" operator from now on and liking Python just a tiny bit less for the inconsistency. So glad they reconsidered.

[1] https://news.ycombinator.com/item?id=19314646

Post reply on HN