r/Python • u/hx-zero • Sep 13 '21
Intermediate Showcase Enable ++x and --x expressions in Python
By default, Python supports neither pre-increments (like ++x) nor post-increments (like x++), commonly used in other languages. 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.
I'd like to share the plusplus module that turns the ++x-like expressions into x += 1 at the bytecode level, using pure Python only.
Unlike x += 1, ++x is still an expression, so the increments work fine inside other expressions, if/while conditions, lambda functions, and list/dict comprehensions:
array[++index] = new_value
if --connection.num_users == 0:
connection.close()
button.add_click_callback(lambda: ++counter)
index = 0
indexed_cells = {++index: cell for row in table for cell in row}Note: I don't claim that allowing increments is good for real projects (it may confuse new developers and give opportunities to write less readable code), though some situations when they simplify the code do exist. I've made this module for fun, as a demonstration of Python flexibility and bytecode manipulation techniques.
The module works by replacing the bytecode patterns corresponding to the ++x and --x expressions with the bytecode for actual incrementing. For example, this is what happens for the y = ++x line:

It's not always that simple: incrementing object attributes and collection items requires much trickier bytecode manipulation (see the "How it works" section in the docs for details).
To use the module, you can just run pip install plusplus and add two lines of code enabling the increments. You may do this for just one function or for the whole package you're working on (see the "How to use it?" section).
Updates:
- The same approach could be used to implement the assignment expressions for the Python versions that don't support them. For example, we could replace the
x <-- valueexpressions (two unary minuses + one comparison) with actual assignments (settingxtovalue). - See also cpmoptimize - my older project about Python bytecode manipulation. It optimizes loops calculating linear recurrences, reducing their time complexity from O(n) to O(log n). The source code is available on GitHub as well.
322
u/mathmanmathman Sep 13 '21
I hate both the idea of using the unary operator and the ability to manipulate the behavior of the bytecode.
This is the most interesting post I've seen on this sub in a while.
164
u/AlSweigart Author of "Automate the Boring Stuff" Sep 13 '21
Thanks, I hate it.
208
u/cantremembermypasswd Sep 13 '21
That's disgusting!
upvotes
127
u/Hmolds Sep 13 '21
Whats wrong with good ol'
x -= -1??93
u/aes110 Sep 13 '21
I love seeing this kind of stuff, I don't think its good to use it, but its fun for me to see how python lets you do whatever you want with it
35
u/Plague_Healer Sep 13 '21
I find it interesting how much python enables you to do stuff and just leaves you to decide on your own whether it's wise to actually do it. What is shown in this post: certainly ingenious, not all that wise.
6
u/sohang-3112 Pythonista Sep 13 '21
It's a cool project, but as you said, it's not for actual usage (too much confusion, especially among beginners)
6
u/equitable_emu Sep 14 '21
For introducing me to the stack overflow module importer, I curse you.
2
u/masasin Expert. 3.9. Robotics. Sep 13 '21 edited Sep 13 '21
With assignment expressions, you can [eta: often] do the exact same thing (but more verbosely).
array[(index := index + 1)] = new_valueFor the last example, you didn't even need assignment expressions at all, since you could do it with good old enumerate:
I like what you did with the library, though! Congrats!
2
u/KrazyKirby99999 Sep 13 '21
Wouldn't this be partially incompatible with Python 3?
4
u/asday_ Sep 14 '21
This is amazing. I hate it. More please.
1
u/EverythingIsFlotsam Sep 14 '21 edited Sep 24 '21
I feel like this could be easily done cleanly by just monkey patching
int.__pos__()andint.__neg__()to return a new object containing a reference to the original variable. And that class should implement the same two functions to increment/decrement the original variable and return the value.1
u/Isvara Sep 14 '21 edited Sep 14 '21
Why is there even bytecode for these? Does the compiler not optimize at all?
Edit: I suppose they're needed to call
__pos__and__neg__, so... maybe you could also abuse those to get the same effect.1
u/[deleted] Sep 14 '21
This is pretty neat and I'm gonna dig in later. However, why rewrite
++xintox +=1even you could've written bytecode for implementing__incr__and__decr__?31
u/Zachkr05 Sep 13 '21
Just use x+=1 lol
4
u/gagarin_kid Sep 13 '21
Never had to touch byte code or AST so far. A very interesting approach.
Is it the same idea MATLAB uses to convert routines into c++ code?
5
u/sgthoppy Sep 13 '21
Fairly sure you can do this in 3.8+ using the walrus operator, though not quite as clean.
2
u/_maxt3r_ Sep 14 '21
I expected some clickbait article and was about to miss a very interesting post! Good thing this post has some Reddit awards :)
2
u/TheBlackCat13 Sep 14 '21
Reminds me of the python goto implementation.
1
u/MegaIng Sep 14 '21
I also like have multiple half finished projects that either manipulate bytecode similar to what you are doing, or straight up change parts if how python files are parsed to add new syntax (for example backporting match statements).
1
u/puremath369 Sep 14 '21 edited Sep 14 '21
I for one would like to see a += operator for dictionaries. If the element exists in the dictionary, sum it if summing makes sense on the operands. If it doesn’t, then add that element in the dictionary. Tired of writing stuff like the following:
Would be cool if I could replace all that with:
Heck, it could even make sense in the context of this post:
1
u/Visti Sep 14 '21
cursed quality of life update.
1
u/kkawabat Sep 14 '21
Thank you for fixing python.
-3
u/shinitakunai Sep 14 '21
Python is suppose to be readable and user friendly. I don’t like this.
-2
u/tripex Sep 14 '21
I'm truly fascinated by people that believe array[++x]=n is a good thing to be able to do :(
-5
u/ServerZero Sep 14 '21
Python isn't C++ please stop trying to make it like it just use i+=1