Postfix/Prefix Operators (++,--)

Peter Hansen peter at engcorp.com
Fri Jun 6 05:26:14 EDT 2003


hostmaster wrote:
> 
> Python doesn't seems to support postfix/prefix operators (e.g. i++,
> --i).  Why?

See Jp's and Chad's answer, and add to that all the *many* past
discussions on the same point (it's not like you've asked an original
question... the archives would almost certainly have helped you),
plus the following:

Python statements are not expressions.  They do *not* return values
as they do in Java and C.  That pretty much removes any possible
purpose in having those operators.  

Secondly, in Python assignment actually *rebinds* a name to a new
object, so i = i + 1 creates a new object that is one greater than
the old, and rebinds the name "i" to it (like a pointer).  The old
value, if there are no more references to it, is destroyed.

This, combined with the fact that these integer objects are actually
*immutable* (they cannot be changed!) pretty much prevents any
possibility of those operators working the way you'd expected.

End result: no chance of having it, and no reason to have it...

-Peter




More information about the Python-list mailing list