Augmented assignment should never modify the assign target!

Bob Alexander balexander at rsv.ricoh.com
Thu Sep 7 15:06:46 EDT 2000


Sorry if this has already been discussed to death.

I was a bit concerned when I saw an early "Augmented Assignment" PEP a
few weeks ago, and now I'm more concerned since the part of that
definition that concerns me is still there (in the "What's New in 2.0b1"
blurb).

I'm a big fan of augmented assignment. However the statement "if A is a
mutable object, A may be modified in place" seems like a bad idea. I
believe that augmented assignment should emulate normal assignment in
its semantics. That is

	target += value

is always equivalent to

	target = target + value

period!

The existing semantics for assignment is that the target object is never
modified by it (unless one of the operations in "value" has that side
effect, of course).

Since the operator + never modifies it operands, += should not either.
Using augmented assignment could have non-intuitive results in certain
cases:

	a = []
	b = a
	a = a + [1]	# this does not modify b
	a += [1]	# this modifies b!

I recommend removing any reference to "in-place" modification as a
result of augmented assignment. If this recommendation is accepted,
there would also be no need for the i-prefixed special methods (iadd,
isub, ...). All semantics could be handled implicitly by the Python
compiler.

IMO, removing the in-place stuff would prevent unnecessary and misguided
complication of the language.

Bob




More information about the Python-list mailing list