Skip to content

Commit d143f97

Browse files
committed
Add space-invader operator case
1 parent 03719de commit d143f97

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,6 +2368,24 @@ nan
23682368
+ `++a` parses as `+(+a)` which translates to `a`. Similarly, the output of the statement `--a` can be justified.
23692369
+ This StackOverflow [thread](https://stackoverflow.com/questions/3654830/why-are-there-no-and-operators-in-python) discusses the rationale behind the absence of increment and decrement operators in Python.
23702370
2371+
* There is a lack of incrementation operator known from different languages, but have you ever heard about _the space-invader operator_?
2372+
```py
2373+
>>> a = 42
2374+
>>> a -=- 1
2375+
>>> a
2376+
43
2377+
```
2378+
2379+
+ together with another:
2380+
```py
2381+
>>> a +=+ 1
2382+
>>> a
2383+
>>> 44
2384+
```
2385+
2386+
**💡 Explanation:**
2387+
This prank comes from [Raymond Hettinger's twit](https://twitter.com/raymondh/status/1131103570856632321?lang=en) and it is actually just malformatted `a -= (-1)`. Which is eqivalent to `a = a - (- 1)`. Similar for the `a += (+ 1)` case.
2388+
23712389
* Python uses 2 bytes for local variable storage in functions. In theory, this means that only 65536 variables can be defined in a function. However, python has a handy solution built in that can be used to store more than 2^16 variable names. The following code demonstrates what happens in the stack when more than 65536 local variables are defined (Warning: This code prints around 2^18 lines of text, so be prepared!):
23722390
```py
23732391
import dis

0 commit comments

Comments
 (0)