The best answers to the question “Behaviour of increment and decrement operators in Python” in the category Dev.
I notice that a pre-increment/decrement operator can be applied on a variable (like
++count). It compiles, but it does not actually change the value of the variable!
What is the behavior of the pre-increment/decrement operators (++/–) in Python?
Why does Python deviate from the behavior of these operators seen in C/C++?
Python does not have pre and post increment operators.
In Python, integers are immutable. That is you can’t change them. This is because the integer objects can be used under several names. Try this:
>>> b = 5 >>> a = 5 >>> id(a) 162334512 >>> id(b) 162334512 >>> a is b True
a and b above are actually the same object. If you incremented a, you would also increment b. That’s not what you want. So you have to reassign. Like this:
b = b + 1
Many C programmers who used python wanted an increment operator, but that operator would look like it incremented the object, while it actually reassigns it. Therefore the
+= operators where added, to be shorter than the
b = b + 1, while being clearer and more flexible than
b++, so most people will increment with:
b += 1
Which will reassign
b+1. That is not an increment operator, because it does not increment
b, it reassigns it.
In short: Python behaves differently here, because it is not C, and is not a low level wrapper around machine code, but a high-level dynamic language, where increments don’t make sense, and also are not as necessary as in C, where you use them every time you have a loop, for example.
++ is not an operator. It is two
+ operators. The
+ operator is the identity operator, which does nothing. (Clarification: the
- unary operators only work on numbers, but I presume that you wouldn’t expect a hypothetical
++ operator to work on strings.)
Which translates to
You have to use the slightly longer
+= operator to do what you want to do:
count += 1
I suspect the
-- operators were left out for consistency and simplicity. I don’t know the exact argument Guido van Rossum gave for the decision, but I can imagine a few arguments:
- Simpler parsing. Technically, parsing
++countis ambiguous, as it could be
+operators) just as easily as it could be
++operator). It’s not a significant syntactic ambiguity, but it does exist.
- Simpler language.
++is nothing more than a synonym for
+= 1. It was a shorthand invented because C compilers were stupid and didn’t know how to optimize
a += 1into the
incinstruction most computers have. In this day of optimizing compilers and bytecode interpreted languages, adding operators to a language to allow programmers to optimize their code is usually frowned upon, especially in a language like Python that is designed to be consistent and readable.
- Confusing side-effects. One common newbie error in languages with
++operators is mixing up the differences (both in precedence and in return value) between the pre- and post-increment/decrement operators, and Python likes to eliminate language “gotcha”-s. The precedence issues of pre-/post-increment in C are pretty hairy, and incredibly easy to mess up.
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
ints are immutable, what happens here is as follows:
- look up the object that
arefers to (it is an
- look up the value of object
- add 1 to that value (1 + 1 = 2)
- create a new
intobject with value
2(it has object id
- rebind the name
ato this new object
arefers to object
0x559239eeb3a0and 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)))
While the others answers are correct in so far as they show what a mere
+ usually does (namely, leave the number as it is, if it is one), they are incomplete in so far as they don’t explain what happens.
To be exact,
+x evaluates to
I could imagine a VERY weird class structure (Children, don’t do this at home!) like this:
class ValueKeeper(object): def __init__(self, value): self.value = value def __str__(self): return str(self.value) class A(ValueKeeper): def __pos__(self): print 'called A.__pos__' return B(self.value - 3) class B(ValueKeeper): def __pos__(self): print 'called B.__pos__' return A(self.value + 19) x = A(430) print x, type(x) print +x, type(+x) print ++x, type(++x) print +++x, type(+++x)