<div dir="ltr">On Thu, Jan 31, 2013 at 8:20 PM, Danny Yoo <span dir="ltr"><<a href="mailto:dyoo@hashcollision.org" target="_blank">dyoo@hashcollision.org</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="im">On Thu, Jan 31, 2013 at 11:36 AM, heathen <<a href="mailto:godsofliberty@lavabit.com">godsofliberty@lavabit.com</a>> wrote:<br>
> why is this:<br>
><br>
</div><div class="im">>>>> d *= 3 + 4<br>
<br>
<br>
</div>The gory details about how Python understands this expression can be found in:<br>
<br>
<a href="http://docs.python.org/3/reference/simple_stmts.html#augmented-assignment-statements" target="_blank">http://docs.python.org/3/reference/simple_stmts.html#augmented-assignment-statements</a><br>
<br>
Technically, "3 + 4" is the "expression list" part of the statement,<br>
so it gets evaluated. Then it does the multiplication of the target<br>
(d) and the value of the expression list (7).<br>
<div class=""><div class="h5"><br></div></div></blockquote><div><br></div><div style>The docs really hit it with that "similar but not exactly equal" phrase. Augmented assignment is one of those things that is sort-of-but-not-really equivalent, so it can really bite you in the ass. I recently encountered this little gem on:</div>
<div style><br></div><div style><div>>>> a = ([], [])</div><div>>>> a[0] += [1]</div><div><br></div><div>Traceback (most recent call last):</div><div> File "<pyshell#1>", line 1, in <module></div>
<div> a[0] += [1]</div><div>TypeError: 'tuple' object does not support item assignment</div><div>>>> a</div><div>([1], [])</div><div>>>> </div><div><br></div><div style>tuples are immutable, but lists *do* support in-place modification. And thus we get an operation that both fails and succeeds at the same time..</div>
</div></div></div></div>