[Tutor] Why x+=y instead of x=x+y?

Yigal Duppen yduppen@xs4all.nl
Fri, 19 Jul 2002 10:54:01 +0200


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

> ...or is there a real
> difference between "x +=y" and "x = x+y" that justifies a notation that
> looks like a typo?

Hi Scot,

As far as I know there is a difference between the expressions in that the += 
notation requires one less lookup; furthermore, objects can have a separate 
method for the 'typo'-version: I believe those are the __iadd__, __isub__ 
etc. methods.

In practice, (especially when doing += on integers) this has no performance 
impact whatsoever; however, years of happy Java coding have slowly ingrained 
the notion that "+=" is read as "is incremented by"; and vice-versa, 
incrementing stuff is automatically done using +=.

I just can't help it, it's stronger than me :-)
But given the absence of any good reason, I think this notation was included 
to keep people like me happy.

YDD
- -- 
.sigmentation Fault
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE9N9OsLsKMuCf5EdwRAoC3AJ40oA7FB9w/8V5fYsfG5c1KFx48MgCfVBiH
ic9g28x/hAAmBVhJL7xmtgQ=
=ulLY
-----END PGP SIGNATURE-----