Python integer incrementing with ++

1,387,742

Solution 1

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

number += 1

Solution 2

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

Solution 3

You can do:

number += 1

Solution 4

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

Solution 5

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.

Share:
1,387,742
Znarkus
Author by

Znarkus

Updated on October 21, 2021

Comments

  • Znarkus
    Znarkus over 2 years

    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?

  • rickcnagy
    rickcnagy about 10 years
    I think that @Thomas's explanation is more useful here; I think the question is more of why and not what.
  • tato
    tato almost 9 years
    useful reference to enumerate() and itertools.count()
  • Darren Ringer
    Darren Ringer almost 9 years
    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.
  • Tom
    Tom over 7 years
    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.
  • alezvic
    alezvic almost 7 years
    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.
  • Artyer
    Artyer over 6 years
    @Tom reserved_indices = itertools.count(); col_names = [name if name != '_' else 'reserved' + str(next(reserved_indices)) for name in column_names]
  • jeffery.yuan
    jeffery.yuan about 6 years
    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 )
  • Dr_Zaszuś
    Dr_Zaszuś over 5 years
    Not exactly. The following will not work as expected: progress = 0; print(progress += 1). So += does not seem to completely replace C++'s ++ operator.
  • jrh
    jrh over 5 years
    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.
  • msouth
    msouth over 4 years
    @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.
  • Gringo Suave
    Gringo Suave about 4 years
    PEP 572 changed much of this.
  • Cecil Curry
    Cecil Curry over 3 years
    PEP 572 currently changes nothing for most of us, because assignment expressions are only available under Python 3.8. Meanwhile, Python 3.5 through 3.7 have yet hit to EOL. If you'd like your code to remain compatible with actively maintained Python versions (and you should), just use the long-standing itertools.count() iterator, which has existed since literally the dawn of time, space, and Python 3 itself, is trivial to use, and comes with no such portability concerns.
  • Christian Lacdael
    Christian Lacdael over 3 years
    are = you[kidding++] << me; are = you[plus(kidding)] <<me;
  • alan2here
    alan2here about 3 years
    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