Re:Dictionary union (PEP 584) is merged
by Znafon on Tuesday February 25, 2020 @05:39PM
(#22418283)
Guido himself said he had forgotten about this trick and since it's syntactic sugar, it does not respect dict subclasses or other mappings.
[ Parent
| on HN ]
Re:Dictionary union (PEP 584) is merged
by seemslegit on Tuesday February 25, 2020 @05:53PM
(#22418405)
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 usedIt’s also really expensive. This new operator works with any Mapping type without needing silly hacks.
Re:Dictionary union (PEP 584) is merged
by anonymoushn on Tuesday February 25, 2020 @10:19PM
(#22419983)
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
by CameronNemo on Tuesday February 25, 2020 @11:29PM
(#22420280)
Not sure about the set operator, but this operator is meant to handle subclasses better than the status quo.
Re:Dictionary union (PEP 584) is merged
by mdrachuk on Wednesday February 26, 2020 @12:55AM
(#22420664)
Wow, one more point for python 3.
Just override ‘__and__’ in your whatever class to replace default return.
Pretty explicit in my book.
Re:Dictionary union (PEP 584) is merged
by blackandblue on Wednesday February 26, 2020 @07:19AM
(#22422299)
the easy to forget justification is surprising to me. especially when most modern languages have the concept of unpacking, rest, spread or etc.
making the trick work with other mapping types and making it faster is totally understandable though.
Re:Dictionary union (PEP 584) is merged
by banannaise on Thursday February 27, 2020 @03:22PM
(#22437369)
It's pretty obvious with the context of other languages, but wildly outside the norm for Python. I rarely see dict unpacking outside function signatures.