<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2013/6/6 Ethan Furman <span dir="ltr"><<a href="mailto:ethan@stoneleaf.us" target="_blank">ethan@stoneleaf.us</a>></span><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+= is an in-place instruction. The only time it doesn't (shouldn't) modify the object is when the object is immutable (such as an int).<br>
<br>
Having a mutable object not be updated in place would be very surprising.<br></blockquote><div><br></div><div>you are right. obj1 += obj2 is different from obj1 = obj1 + obj2, because it just calls a method (__iadd__) on obj1 instead of calling a method (__add__) AND reassigning the name “obj1” to that method’s return value.<br>
<br><div class="gmail_extra"><div class="gmail_quote">2013/6/6 Devin Jeanpierre <span dir="ltr"><<a href="mailto:jeanpierreda@gmail.com" target="_blank">jeanpierreda@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Can you name a language other than Python where `a += b` usually does
something different for mutable values from `a = a + b`, because of a
difference in how the + and += operators work w.r.t. if/how they
mutate their arguments? I can't think of any.<span class=""></span><br></blockquote><div><br></div><div>scala. it works exactly like python, i.e. that += mutates objects without rebinding the variable name, and + computing a new value and binding the variable to it.<br>
<br></div><div>e.g.:<br><br><div>val s = StringBuffer("mutable constant")<br></div><div>s += "foo" //s.+=("foo") is called</div><br><div>var s = "immutable variable"<br></div>s += "foo" //Straing has no method “+=”, so it gets transformed to s = s + "foo"<br>
</div><div><br></div><div>val s = "immutable constant"<br></div><div>s += "foo" //String has no method “+=”, so it gets transformed to s = s + "foo" ⇒ compile error due to trying to rebind constant</div>
</div></div></div></div></div></div>