<div dir="ltr"><div dir="ltr"><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Mar 4, 2019 at 3:31 PM Del Gan <<a href="mailto:delgan.py@gmail.com">delgan.py@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">
> the augmented assignment version allows anything the ``update`` method allows, such as iterables of key/value pairs<br>
<br>
I am a little surprised by this choice.<br>
<br>
First, this means that "a += b" would not be equivalent to "a = a +<br>
b". Is there other built-in types which act differently if called with<br>
the operator or augmented assignment version?<br></blockquote><div><br></div><div>Yes. The same happens for lists. [1] + 'a' is a TypeError, but a += 'a' works:</div><div><br>>>> a = [1]<br>>>> a + 'a'<br>Traceback (most recent call last):<br>  File "<stdin>", line 1, in <module><br>TypeError: can only concatenate list (not "str") to list<br>>>> a += 'a'<br>>>> a<br>[1, 'a']<br>>>> <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">
Secondly, that would imply I would no longer be able to infer the type<br>
of "a" while reading "a += [('foo', 'bar')]". Is it a list? A dict?<br></blockquote><div><br></div><div>Real code more likely looks like "a += b" and there you already don't have much of a clue -- the author of the code should probably communicate this using naming conventions or type annotations.<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">
Those two points make me uncomfortable with "+=" strictly behaving<br>
like ".update()".<br></blockquote><div><br></div><div>And yet that's how it works for lists. (Note that dict.update() still has capabilities beyond +=, since you can also invoke it with keyword args.)</div><div><br></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></div>