integers

Greg Ewing greg at cosc.canterbury.ac.nz
Thu Jun 7 23:37:31 EDT 2001


Alan Daniels wrote:
> 
> I believe the new "x += 1" syntax is just synactic sugar which serves
> as shorthand for "x = x + 1"

Actually, it's syntactic sugar for something
more like

  x = x.__iadd__(1)

In the case of integers (and other immutable types)
this happens to be the same as x = x + 1. But mutable
types have the option of implementing __iadd__ so
that it changes the object in-place and then returns
a reference to the same object. So, for example, if
a and b are NumPy matrices, a += b can be nice and
efficient.

-- 
Greg Ewing, Computer Science Dept, University of Canterbury,	  
Christchurch, New Zealand
To get my email address, please visit my web page:	  
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list