From: Christopher B. <Chr...@no...> - 2006-10-19 21:00:21
|
Robert Kern wrote: > From the reference manual: > > http://docs.python.org/ref/augassign.html > > """An augmented assignment expression like x += 1 can be rewritten as x = x + 1 > to achieve a similar, but not exactly equal effect. In the augmented version, x > is only evaluated once. Also, when possible, the actual operation is performed > in-place, meaning that rather than creating a new object and assigning that to > the target, the old object is modified instead.""" I've always thought this was a mistake -- it is a source of weird errors and bugs. AFAIC, the augmented assignments should ONLY work with mutable objects, and ALWAYS do the operation in place. But then you couldn't write: i = 1 i += 1 Because python numbers are immutable. I think the problem here is that augmented assignment is solving two distinct problems: in place operations and nice syntax for incrementing. Oh well, it's not that big a deal, and usually the confusion causes errors that show up right away. -Chris -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chr...@no... |