Enables increment operators in Python with a bytecode hack
![](https://www.deeplearningdaily.com/wp-content/uploads/2021/09/enables-increment-operators-in-python-with-a-bytecode-hack_614663a8e6e98-375x210.jpeg)
Enable ++x
and --x
expressions in Python
What’s this?
By default, Python supports neither pre-increments (like ++x
) nor post-increments (like x++
). However, the first ones are syntactically correct since Python parses them as two subsequent +x
operations, where +
is the unary plus operator (same with --x
and the unary minus). They both have no effect, since in practice -(-x) == +(+x) == x
.
This module turns the ++x
-like expressions into x += 1
at the bytecode level. Increments and decrements of collection items and object attributes are supported as well, for example: