autoincrementing

Alex Martelli aleax at aleax.it
Tue Feb 25 08:09:14 EST 2003


Dale Strickland-Clark wrote:
   ...

> That is not the point of auto-increment (and I'm sure you must know
> this)
> 
>    intPrev = intNew++
> 
> is equivalent to:
> 
>    intPrev = intNew
>    intNew += 1
> 
> Rather more than one character saved. And there are times when it can
> save you a lot more than that, too.

In Python, all binding and rebinding of names is in statements, never in 
expressions; and numbers are immutable -- so if x refers for example to
the number 3, there is no way to make x refer to the number 4 except by
rebinding name x to a different object (you cannot ALTER a number).

I had not imagined the original poster was asking for Python to be
entirely and drastically revolutionized -- making numbers mutable, or
letting name binding or rebinding happen in expressions and not just
in statements -- in order to let him save A FEW characters' worth of
typing; I had rather made more charitable assumptions about what he
was asking about/for.

If the question being asked is "why are numbers immutable" or "why
does Python distinguish between expressions and statements and never
lets operators bind or rebind the names they're applied to", then
the only sensible answer is "because it's Python -- if you want C,
you know where to find it".

If the question is "why doesn't Python make an exception to its
otherwise simple and universal rules in order to treat an extremely
specific case 'ad hoc' in a way that would be convenient to me",
then my suggestion would be to switch to Perl: in that language you
will find an endless, dazzling variety of such "convenient" design
choices -- bon appetit!

Since I keep assuming human being are fundamentally good, even though
sometimes misguided, I still prefer to keep also assuming that the
poster, as indicated by using the non-C terminology "autoincrement"
rather than the C terminology "preincrement" or "postincrement", was
NOT wondering about inflicting such horrors upon Python, but simply
about a "++" synonym for the construct "+=1" -- a statement rebinding
a reference, but NOT returning a result.


Alex






More information about the Python-list mailing list