<div dir="ltr"><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Mar 15, 2019 at 9:19 PM Inada Naoki <<a href="mailto:songofacandy@gmail.com">songofacandy@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Sat, Mar 16, 2019 at 2:51 AM Guido van Rossum <<a href="mailto:guido@python.org" target="_blank">guido@python.org</a>> wrote:<br>
><br>
> But I think that the folks who point out "there is already a way to do this" are missing the point that it really is easier to grasp the meaning of this:<br>
><br>
>     d = d1 + d2<br>
><br>
> compared to this:<br>
><br>
>     d = d1.copy()<br>
>     d = d1.update(d2)<br></blockquote><div><br></div><div>[Note that I made a typo in the last line. It should be `d.update(d2)`, no assignment.]<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> and it is not just a matter of fewer lines of code: the first form allows us to use our visual processing to help us see the meaning quicker -- and without distracting other parts of our brain (which might already be occupied by keeping track of the meaning of d1 and d2, for example).<br>
<br>
It seems this example is bit unfair.  It is not just method vs operator,<br>
because dict doesn't provide outer place version of update() method.<br></blockquote><div><br></div><div>Actually most of my post was exactly about why operators can in some cases be better than functions (which includes methods).<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
In case of set, `s = s1 | s2` can be compared to `s = s1.union(s2)`.<br>
<br>
So dict example doesn't explain "why add operator instead of method?"<br></blockquote><div><br></div><div>Correct, since most of the post was already explaining it. :-) <br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> Of course, everything comes at a price. You have to learn the operators, and you have to learn their properties when applied to different object types. (This is true in math too -- for numbers, x*y == y*x, but this property does not apply to functions or matrices; OTOH x+y == y+x applies to all, as does the associative law.)<br>
<br>
I think behavior is more important than properties.<br>
When we learn operator's behavior, its property is obvious.<br>
So main point of using operator or not is consistency.  Same operator<br>
should be used for same thing as possible.<br>
<br>
I prefer | to + because the behavior of dict.update() looks similar set.union()<br>
rather than list.extend().<br></blockquote><div><br></div><div>That's a separate topic and I did not mean to express an opinion on it in this post. I simply used + because it's the simplest of all operators, and it makes it easier for everyone to follow the visual argument.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Another option I like is add + operator to not only dict, but also set.<br>
In this case, + is used to join containers by the way most natural to the<br>
container's type.<br>
<br>
That's what Kotlin and Scala does.  (Although Scala used ++ instead of +).<br>
ref: <a href="https://discuss.python.org/t/pep-584-survey-of-other-languages-operator-overload/977" rel="noreferrer" target="_blank">https://discuss.python.org/t/pep-584-survey-of-other-languages-operator-overload/977</a></blockquote><div><br></div><div>This probably belongs in another thread (though IIRC it has been argued to death already).<br></div><div> </div></div>-- <br><div dir="ltr" class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div></div>