1032

I've always laughed to myself when I've looked back at my VB6 days and thought, "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? Don't people use the ++ / -- notation?

16
  • 11
    I for one am quite happy that we don't have to put up with things like a[i] = i++; where the order of evaluation in C++ is undefined. Commented Apr 13, 2010 at 19:51
  • 5
    Answers to your questions in the given order: “Erlang, Python, Lua etc” (for "modern" meaning after the creation of C); “No”; and “Not necessarily”. Commented Apr 13, 2010 at 22:04
  • 87
    Disagree with y'all: i++ is less to write so less to read. Less to read means brain can focus more on the big picture. Commented Feb 5, 2016 at 15:26
  • 12
    @TimPietzcker: a better solution would be to well define the evaluation order, probably from left to right, rather than dropping a useful operator. And to the OP: Python is hardly a modern language... and is a quite crappy language actually, despite being widely used. Commented Jun 27, 2016 at 13:43
  • 8
    @uoɥʇʎPʎzɐɹC Neither is particularly complex and neither is ugly. The first one is quicker to understand. Commented Jul 20, 2016 at 10:19

7 Answers 7

1783

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

number += 1
Sign up to request clarification or add additional context in comments.

4 Comments

I think that @Thomas's explanation is more useful here; I think the question is more of why and not what.
Agreed with @rickcnagy, more like the "how to do it?" (if you really don't care about brevity of code you could also simply do number = number + 1) the reasoning on why ++ and -- don't exist in Python seems more useful.
Not exactly. The following will not work as expected: progress = 0; print(progress += 1). So += does not seem to completely replace C++'s ++ operator.
Python has loads of ?= where ? is replaced by another operator, although it wont work with every operator. For example n \= 2 becomes n = n \ 2, however n +== 1 doesn't unpack to n = n += 1
553

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().

8 Comments

useful reference to enumerate() and itertools.count()
Care to suggest an elegant replacement for this: reserved_index = 0; col_names = [name if name != '_' else 'reserved' + (reserved_index++) for name in column_names]? I'm passed a list of column names where some that are not interesting are just '_'. I need to construct a temporary table with those '_'s replaced with unique but non-meaningful names. The in-place postincrement operator would make this easy; I'm struggling to come up with something else that doesn't involve looping over the array explicitly.
@Tom reserved_indices = itertools.count(); col_names = [name if name != '_' else 'reserved' + str(next(reserved_indices)) for name in column_names]
This decision violates the rule - no surprise. ++number works in most of languages. - How many bugs this may cause. The language should be also developer friendly )
@jeffery.yuan Been at it less than a year (meaning python--I've coded for a couple decades plus), but, man, the number of things like that...boggles the mind. Lowest common denominator kinda won.
|
69

You can do:

number += 1

Comments

39

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

2 Comments

It makes writing UI code so much less line-y though. Well other than the fact that using Python cuts your lines to like 15% what they'd be in C++, heh.
--Guido......
27

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.

1 Comment

Sometimes I find that you really do just need iteration indexes, e.g., if you want to track how many times a function runs before it converges, etc. Though maybe that still counts as "seldom used", Python is pretty well suited for the most part to scientific coding, FWIW.
13

Take a look at Behaviour of increment and decrement operators in Python 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()

Comments

10

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.