Earlier quoted context omitted.
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).
Dictionary union (PEP 584) is merged
31–40 of 87 posts
Re: Dictionary union (PEP 584) is merged
#32Re: Dictionary union (PEP 584) is merged
#33Re: Dictionary union (PEP 584) is merged
#34> The new operators will have the same relationship to the dict.update method as the list concatenate (+) and extend (+=) operators have to list.extend. Note that this is somewhat different from the relationship that |/|= have with set.update; the authors have determined that allowing the in-place operator to accept a wider range of types (as list does) is a more useful design, and that restricting the types of the binary operator's operands (again, as list does) will help avoid silent errors caused by complicated implicit type casting on both sides.
Would someone please explain what they mean with regard to being different from set.update, and what could lead to silent errors?
Re: Dictionary union (PEP 584) is merged
#35Earlier quoted context omitted.
That ship had long sailed with string formatting anyways.
It sailed long before that.
The last died with 3.0.
But my point is that the Zen of Python must be seen as a post hoc description overlaid onto whatever the actual Python philosophy is. Aligned, certainly, but at times only roughly aligned.
So I don't see it as having sailed (with string formatting, or other specific even) but never having been there in the first place. More like, sailing in the same waters.
Re: Dictionary union (PEP 584) is merged
#36Re: Dictionary union (PEP 584) is merged
#37The problem with sprinkling operator overloading all over the place in non numerical use is that you as tje reader don't get the context hints provided by method names. I think this change is bad in the overall balance.
{**d1, **d2}
This provides a clearer symbolic notation for dictionaries analogous to what's already available with sets. FWIW the pep discusses what this would look like as a method vs an operator:Re: Dictionary union (PEP 584) is merged
#38Earlier quoted context omitted.
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
There has been a recent effort to add all these new operators that don't actually let you do anything you couldn't but now you can confuse everyone by doing it in other ways.
Re: Dictionary union (PEP 584) is merged
#39Earlier quoted context omitted.
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).
Just override ‘__and__’ in your whatever class to replace default return.
Pretty explicit in my book.
Re: Dictionary union (PEP 584) is merged
#40The problem with sprinkling operator overloading all over the place in non numerical use is that you as tje reader don't get the context hints provided by method names. I think this change is bad in the overall balance.
The best way to do dictionary union is already symbolic: {**d1, **d2} This provides a clearer symbolic notation for dictionaries analogous to what's already available with sets. FWIW the pep discusses what this would look like as a method vs an operator: https://www.python.org/dev/peps/pep-0584/#use-a-method