2

I come from a C/C++ background and I keep on typing things like

ix = -1
fred = objlist[++ix].value

This doesn't work because there is no preincrement operator in python. It just gives me the item with index -1. That is OK - I know how to fix that.

In fact, I just found out recently when I fell asleep at the keyboard that it allows

------------------ix

It also allows

++++++++++++++++++ix

What I can't figure out is why the python syntax allows ++ix and --ix. The syntax doesn't allow ix++ or ix--.

3
  • 5
    ++ix in Python is equivalent to +(+ix) (i.e. ix). Similar for -(-ix). stackoverflow.com/questions/774784/… / stackoverflow.com/questions/470139/why-does-12-3 Commented Feb 23, 2017 at 8:27
  • Try searching for unary + operator in python Commented Feb 23, 2017 at 8:29
  • 1
    In Python integers are not mutable, the operator would be ineffective. To increment one, use ix += 1 which is equivalent to ix = ix + 1. Commented Feb 23, 2017 at 8:36

1 Answer 1

4

integers in python are immutable and that is why post increment is not allowed and pre increment does not work.

And since integers are immutable, the only way to modify the, is by reassigning them like this:

x += 1

++ is not an operator. It is two + operators. The + operator is the identity operator, which does nothing which is why ++x does not effect the variable.

To clarify:

++x

parses to +(+x) Which translates to x

In practice the identiy operator + is not used often. Here is the definition in the Python documentation:

The unary + (plus) operator yields its numeric argument unchanged.

Here is an example I found in StackOverFlow where it is used with decimal rounding:

>>> from decimal import Decimal
>>> obj = Decimal('3.1415926535897932384626433832795028841971')
>>> +obj != obj  # The __pos__ function rounds back to normal precision
True
>>> obj
Decimal('3.1415926535897932384626433832795028841971')
>>> +obj
Decimal('3.141592653589793238462643383')

Regarding post increment: Since this operator is not defined in Python, x++ gives a syntax error as the parser can't make sense of this expression.

IMHO the Pyton should give a WARNING when a programmer does ++ because it can lead to many errors on the part of C/C++/C# developers whose intent is to do a pre increment on a variable.

Sign up to request clarification or add additional context in comments.

3 Comments

Maybe we just need something in idle - that is where most people do their experiments. Possibly something like C++ programmer eh? You cannot do that in Python
The immutability of integers is not related to whether or not it allows the syntactic sugar of pre- and post- increment. It's down to design decisions by Guido, see also: stackoverflow.com/questions/3654830/…
++x is semantically equivalent to x += 1 in C and other languages with the pre-increment operator. If ++x couldn't work in Python because of immutability, then x += 1 also shouldn't work because of immutability. Really, int values are immutable in C too, because if you do int x = 1; and int y = x;, then nothing you do to x can change the value of y.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.