Behaviour of increment and decrement operators in Python

Viewed 1357873

How do I use pre-increment/decrement operators (++, --), just like in C++?

Why does ++count run, but not change the value of the variable?

11 Answers

TL;DR

Python does not have unary increment/decrement operators (--/++). Instead, to increment a value, use

a += 1

More detail and gotchas

But be careful here. If you're coming from C, even this is different in python. Python doesn't have "variables" in the sense that C does, instead python uses names and objects, and in python ints are immutable.

so lets say you do

a = 1

What this means in python is: create an object of type int having value 1 and bind the name a to it. The object is an instance of int having value 1, and the name a refers to it. The name a and the object to which it refers are distinct.

Now lets say you do

a += 1

Since ints are immutable, what happens here is as follows:

  1. look up the object that a refers to (it is an int with id 0x559239eeb380)
  2. look up the value of object 0x559239eeb380 (it is 1)
  3. add 1 to that value (1 + 1 = 2)
  4. create a new int object with value 2 (it has object id 0x559239eeb3a0)
  5. rebind the name a to this new object
  6. Now a refers to object 0x559239eeb3a0 and the original object (0x559239eeb380) is no longer refered to by the name a. If there aren't any other names refering to the original object it will be garbage collected later.

Give it a try yourself:

a = 1
print(hex(id(a)))
a += 1
print(hex(id(a)))

In python 3.8+ you can do :

(a:=a+1) #same as ++a (increment, then return new value)
(a:=a+1)-1 #same as a++ (return the incremented value -1) (useless)

You can do a lot of thinks with this.

>>> a = 0
>>> while (a:=a+1) < 5:
    print(a)

    
1
2
3
4

Or if you want write somthing with more sophisticated syntaxe (the goal is not optimization):

>>> del a
>>> while (a := (a if 'a' in locals() else 0) + 1) < 5:
    print(a)

    
1
2
3
4

It will return 0 even if 'a' doesn't exist without errors, and then will set it to 1

There are no post/pre increment/decrement operators in python like in languages like C.

We can see ++ or -- as multiple signs getting multiplied, like we do in maths (-1) * (-1) = (+1).

E.g.

---count

Parses as

-(-(-count)))

Which translates to

-(+count)

Because, multiplication of - sign with - sign is +

And finally,

-count

Extending Henry's answer, I experimentally implemented a syntax sugar library realizing a++: hdytto.

The usage is simple. After installing from PyPI, place sitecustomize.py:

from hdytto import register_hdytto
register_hdytto()

in your project directory. Then, make main.py:

# coding: hdytto

a = 5
print(a++)
print(++a)
b = 10 - --a
print(b--)

and run it by PYTHONPATH=. python main.py. The output will be

5
7
4

hdytto replaces a++ as ((a:=a+1)-1) when decoding the script file, so it works.

A straight forward workaround

c = 0
c = (lambda c_plusplus: plusplus+1)(c)
print(c)
1

No more typing

 c = c + 1

Also, you could just write c++ and finish all your code and then do search/replace for "c++", replace with "c=c+1". Just make sure regular expression search is off.

Related