Sujet : Re: Two aces up Python's sleeve
De : annada (at) *nospam* tilde.green (Annada Behera)
Groupes : comp.lang.pythonDate : 07. Nov 2024, 08:25:53
Autres entêtes
Organisation : tilde.green
Message-ID : <050c2ce9efd8442fb902ecc926afb1ee42fe6c34.camel@tilde.green>
References : 1 2
User-Agent : Evolution 3.52.2
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.