[Python-ideas] Allow using ** twice

Philipp A. flying-sheep at web.de
Thu Jun 6 21:23:28 CEST 2013


2013/6/6 Ethan Furman <ethan at stoneleaf.us>

> += 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).
>
> Having a mutable object not be updated in place would be very surprising.
>

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.

2013/6/6 Devin Jeanpierre <jeanpierreda at gmail.com>

> 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.
>

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.

e.g.:

val s = StringBuffer("mutable constant")
s += "foo" //s.+=("foo") is called

var s = "immutable variable"
s += "foo" //Straing has no method “+=”, so it gets transformed to s = s +
"foo"

val s = "immutable constant"
s += "foo" //String has no method “+=”, so it gets transformed to s = s +
"foo" ⇒ compile error due to trying to rebind constant
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130606/a734141b/attachment.html>


More information about the Python-ideas mailing list