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

cup
  • 7,589
  • 4
  • 19
  • 42
  • 5
    `++ix` in Python is equivalent to `+(+ix)` (i.e. `ix`). Similar for `-(-ix)`. http://stackoverflow.com/questions/774784/is-it-possible-to-overload-operators-in-python/774791#774791 / http://stackoverflow.com/questions/470139/why-does-12-3 – kennytm Feb 23 '17 at 08:27
  • Try searching for unary + operator in python – Selçuk Cihan Feb 23 '17 at 08: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`. – Klaus D. Feb 23 '17 at 08:36

1 Answers1

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.

tjati
  • 5,761
  • 4
  • 41
  • 56
Dave S
  • 973
  • 9
  • 17
  • 1
    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** – cup Feb 23 '17 at 09:20
  • 1
    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: http://stackoverflow.com/questions/3654830/why-are-there-no-and-operators-in-python – Jack Aidley Feb 23 '17 at 12:10
  • `++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`. – kaya3 Jan 31 '22 at 06:52