increment/decrement operators

Ian Kelly ian.g.kelly at gmail.com
Mon Dec 7 19:24:44 EST 2015


On Dec 5, 2015 10:21 AM, "BartC" <bc at freeuk.com> wrote:

>

>

> The latter is not the same. Some of the differences are:

>

> * ++ and -- are often inside inside expressions and return values (unlike
x+=1 in Python)

>

> * x++ and x-- return the /current/ value of x, unlike x+=1 even if it
were to return a value; it would be the new value

Since x+=1 is not an expression, this is moot.

> * x+=1 requires you to hard-code the value 1, but ++ is not necessarily
stepping by 1. You can imagine ++ stepping something to its next value.

Note that x+=1 does not necessarily have to mean stepping by 1 either. You
could even do something like x+=5 to mean skip the next four values and
step x to the value after that.

> However, if ++ and -- are only used as statements, then why not simply
map them to x+=1? In Python, that would need to be x++ and x-- as ++x or
--x have existing meanings.

I think a better question is if they're only going to be statements, then
why bother adding them? x++ doesn't give you anything you can't get from
x+=1, so why commit to supporting the extra syntax?



More information about the Python-list mailing list