Two aces up Python's sleeve

Annada Behera annada at tilde.green
Thu Nov 7 02:25:53 EST 2024


>Then please explain why I have to write:
>
>    i += 1
>
>Instead of the shorter:
>
>    i ++
>
>My short-term memory is really stressed.

I heard this behavior is because python's integers are immutable.
For example:

    >>> x,y = 5,5
    >>> id(x) == id(y)
    True

5 is a object that x and y points to. ++x or x++ will redefine 5 to
6, which the interpreter forbids to keep it's state mathematically
consistent. Also, by not supporting x++ and ++x, it avoids the pre-
and post-increment (substitute-increment v. increment-substitute) bugs
that plagues C and it's children.




More information about the Python-list mailing list