tags:

views:

347

answers:

9

Possible Duplicate:
Python: Behaviour of increment and decrement operators

I've always laughed to myself when I looked back at my VB6 days, "What modern language doesn't allow incrementing with double plus signs?":

number++

To my surprise I can't find anything about this in the Python docs. Must I really subject myself to number = number + 1? Doesn't people use the ++/-- notation?

:-(

+19  A: 

Python doesn't support ++, but you can do:

number += 1
Daniel Stutzbach
+2  A: 

Yes. The ++ operator is not available in Python. Guido doesn't like these operators.

Pierre-Antoine LaFayette
Maybe Guido didn't want anybody inventing `Python++` ;) lol
TheMachineCharmer
+5  A: 

You can do:

number += 1
knutin
+3  A: 

You can use:

number += 1
compie
+3  A: 

Take a look at http://stackoverflow.com/questions/1485841/python-behaviour-of-increment-and-decrement-operators for an explanation of why this doesn't work.

Python doesn't really have ++ and --, and I personally never felt it was such a loss.

I prefer functions with clear names to operators with non-always clear semantics (hence the classic interview question about ++x vs. x++ and the difficulties of overloading it). I've also never been a huge fan of what post-incrementation does for readability.

You could always define some wrapper class (like accumulator) with clear increment semantics, and then do something like x.increment() or x.incrementAndReturnPrev()

Uri
A: 

I've only seen ++ and -- operators in languages that were derived from C. Are there any counter-examples to this?

Ferruccio
I believe perl would be a counterexample.
Pierre-Antoine LaFayette
In what world is perl's syntax not derived from C?
Wooble
"Perl borrows features from other programming languages including C, shell scripting (sh), AWK, and sed" (wiki). It is not a derivative of C.
Pierre-Antoine LaFayette
+1  A: 

Here there is an explanation: http://bytes.com/topic/python/answers/444733-why-there-no-post-pre-increment-operator-python

However the absence of this operator is in the python philosophy increases consistency and avoids implicitness.

In addition, this kind of increments are not widely used in python code because python have a strong implementation of the iterator pattern plus the function enumerate.

pygabriel
+8  A: 

Simply put, the ++ and -- operators don't exist in Python because they wouldn't be operators, they would have to be statements. All namespace modification in Python is a statement, for simplicity and consistency. That's one of the design decisions. And because integers are immutable, the only way to 'change' a variable is by reassigning it.

Fortunately we have wonderful tools for the use-cases of ++ and -- in other languages, like enumerate() and itertools.count().

Thomas Wouters
+2  A: 

The main reason ++ comes in handy in C-like languages is for keeping track of indices. In Python, you deal with data in an abstract way and seldom increment through indices and such. The closest-in-spirit thing to ++ is the next method of iterators.

Mike Graham