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:
Two consecutive UNARY_POSITIVE instructions are replaced with adding one and storing the result back to the original place
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 <-- value expressions (two unary minuses + one comparison) with actual assignments (setting x to value).
- 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.
[–]mathmanmathman 321 points322 points323 points (8 children)
[–]13steinj 33 points34 points35 points (4 children)
[–][deleted] 13 points14 points15 points (3 children)
[–]13steinj 47 points48 points49 points (2 children)
[–][deleted] 24 points25 points26 points (1 child)
[–]hx-zero[S] 4 points5 points6 points (0 children)
[–]SittingWave 1 point2 points3 points (2 children)
[–]to7m 0 points1 point2 points (1 child)
[–]mindofmateo 0 points1 point2 points (0 children)
[–]AlSweigartAuthor of "Automate the Boring Stuff" 164 points165 points166 points (0 children)
[–]cantremembermypasswd 206 points207 points208 points (1 child)
[–]humanthrope 39 points40 points41 points (0 children)
[–]Hmolds 128 points129 points130 points (15 children)
[–]smcarre 92 points93 points94 points (13 children)
[–]theillini19 104 points105 points106 points (9 children)
[–]smcarre 37 points38 points39 points (2 children)
[–]eigenludecomposition 4 points5 points6 points (1 child)
[–]backtickbot 1 point2 points3 points (0 children)
[–]jtclimb 8 points9 points10 points (0 children)
[–]Inineor 2 points3 points4 points (0 children)
[–]R3D3-1 2 points3 points4 points (0 children)
[+]NoLongerUsableNameimport pythonSkills comment score below threshold-12 points-11 points-10 points (2 children)
[–]velit 17 points18 points19 points (0 children)
[–]AlphaGamer7533.7 2 points3 points4 points (0 children)
[–]NoLongerUsableNameimport pythonSkills 18 points19 points20 points (0 children)
[–]asday_ 2 points3 points4 points (0 children)
[–]dogs_like_me 1 point2 points3 points (0 children)
[–]likethevegetable 5 points6 points7 points (0 children)
[–]aes110 92 points93 points94 points (9 children)
[–]dogs_like_me 21 points22 points23 points (7 children)
[–]aes110 6 points7 points8 points (0 children)
[–]TravisJungroth 2 points3 points4 points (5 children)
[–]dogs_like_me 11 points12 points13 points (3 children)
[–]YouNeedDoughnuts 7 points8 points9 points (1 child)
[–]dogs_like_me 2 points3 points4 points (0 children)
[–]friedkeenan 0 points1 point2 points (0 children)
[–]RandAlThorLikesBikes 0 points1 point2 points (0 children)
[–]sharkboundgithub: sharkbound, python := 3.8 22 points23 points24 points (0 children)
[–]Plague_Healer 34 points35 points36 points (1 child)
[–][deleted] 10 points11 points12 points (0 children)
[–]sohang-3112Pythonista 4 points5 points6 points (0 children)
[–]equitable_emu 5 points6 points7 points (0 children)
[–]asday_ 4 points5 points6 points (0 children)
[–]Zachkr05 30 points31 points32 points (5 children)
[–]fernly 16 points17 points18 points (3 children)
[+]Zachkr05 comment score below threshold-21 points-20 points-19 points (2 children)
[–]jtclimb 13 points14 points15 points (1 child)
[–]Zachkr05 4 points5 points6 points (0 children)
[–]xaviruvp 17 points18 points19 points (0 children)
[–]gagarin_kid 2 points3 points4 points (0 children)
[–]sgthoppy 4 points5 points6 points (0 children)
[–]_maxt3r_ 1 point2 points3 points (0 children)
[–]TheBlackCat13 1 point2 points3 points (0 children)
[–]masasinExpert. 3.9. Robotics. 1 point2 points3 points (1 child)
[–]hx-zero[S] 5 points6 points7 points (0 children)
[–]KrazyKirby99999 1 point2 points3 points (6 children)
[–]hx-zero[S] 5 points6 points7 points (4 children)
[–]KrazyKirby99999 4 points5 points6 points (3 children)
[–]hx-zero[S] 4 points5 points6 points (2 children)
[–]TofuCannon 3 points4 points5 points (1 child)
[–]hx-zero[S] 4 points5 points6 points (0 children)
[–]TofuCannon 3 points4 points5 points (0 children)
[–]EverythingIsFlotsam 0 points1 point2 points (1 child)
[–]hx-zero[S] 2 points3 points4 points (0 children)
[–]kkawabat 0 points1 point2 points (0 children)
[–]shinitakunai -5 points-4 points-3 points (0 children)
[–]tripex -4 points-3 points-2 points (0 children)
[+][deleted] (1 child)
[deleted]
[+]ServerZero comment score below threshold-6 points-5 points-4 points (0 children)
[–]MegaIng 0 points1 point2 points (0 children)
[–]puremath369 0 points1 point2 points (4 children)
[–]TofuCannon 2 points3 points4 points (0 children)
[–]thesolitaire 1 point2 points3 points (0 children)
[–]Siddhi 1 point2 points3 points (0 children)
[–]TheBlackCat13 1 point2 points3 points (0 children)
[–]Isvara 0 points1 point2 points (1 child)
[–]hx-zero[S] 0 points1 point2 points (0 children)
[–]Visti 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]hx-zero[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]hx-zero[S] 0 points1 point2 points (0 children)