In this satirical short article, the author shows how to create Python programs using the built-in exec
and the snippet of code --(...==...)
, which evaluates to 1
:
print(--(...==...)) # 1
The comparison ...==...
evaluates to True
and the double negation converts to the integer 1
by going from True
to -1
, and then to 1
.
To save one character, the author could have opted for +(...==...)
, which also gives 1
.
By combining multiple together, the author produces larger integers:
print(
--(...==...)--(...==...)--(...==...)
) # 3
The author also points out that the leading --
can be ommitted:
print(
(...==...)--(...==...)--(...==...)
) # 3
Finally, by using exec
and string formatting with the format specifier %c
, the author can write arbitrary strings which can then be executed.
The author presents this code:
exec('%c'*((...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...))%(((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)),((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...))--((...==...)--(...==...)),((...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)),((...==...)--(...==...)--(...==...))**((...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...))--((...==...)--(...==...)),((...==...)--(...==...)--(...==...)--(...==...))**((...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...))--((...==...)--(...==...)--(...==...)--(...==...)),((...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...)),((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...))**((...==...)--(...==...))--((...==...)--(...==...)--(...==...)),((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...))**((...==...)--(...==...))--((...==...)--(...==...)--(...==...)--(...==...)),((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...))**((...==...)--(...==...))--(...==...),*(((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...))**((...==...)--(...==...)--(...==...)),)*((...==...)--(...==...)),((...==...)--(...==...)--(...==...))**((...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...))--((...==...)--(...==...)--(...==...)),((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...))--((...==...)--(...==...)),((...==...)--(...==...))**((...==...)--(...==...)--(...==...)--(...==...)--(...==...)),((...==...)--(...==...)--(...==...)--(...==...))**((...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...))--((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)),((...==...)--(...==...)--(...==...))**((...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...))--((...==...)--(...==...)--(...==...)),((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...))--((...==...)--(...==...)),((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...))**((...==...)--(...==...)--(...==...)),((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...))**((...==...)--(...==...)),((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...))**((...==...)--(...==...))--((...==...)--(...==...)--(...==...)),((...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...))--(...==...)))
The beginning sets up the string that will represent the code to be executed:
exec(
'%c'
* ((...==...)--(...==...)--(...==...))
* ((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...))
% (...)
)
This is equivalent to
exec(
'%c'
* 3
* 7
% (...)
)
The part '%c' * 3 * 7
creates a string with length 21: '%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c'
.
The part % (...)
creates a tuple with 21 integers that will be formatted onto that string.
For example, the character "a"
corresponds to the Unicode code point 97, so "%c" % 97
gives "a"
:
>>> ord("a")
97
>>> "%c" % 97
'a'
This means the author wants to run a Python program that is 21 characters in length.
The 21 integers that fill up the source code are all presented linearly inside the given tuple, except for a small section of the code that looks like this:
exec(
'%c'
* ((...==...)--(...==...)--(...==...))
* ((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...))
% (
...,
...,
...,
...,
...,
...,
...,
...,
...,
,*(
((...==...)--(...==...)--(...==...)--(...==...))
* ((...==...)--(...==...)--(...==...))
** ((...==...)--(...==...)--(...==...)),
)
* ((...==...)--(...==...)),
...,
...,
...,
...,
...,
...,
...,
...,
...,
...
)
)
That small section unpacks another tuple inside the main tuple, and the inner tuple is a 1-element tuple that is multiplied by 2 ((...==...)--(...==...)
) to create a 2-element tuple with two equal values.
Here are the 21 integers the author defines, counting twice the one that is duplicated inside an inner tuple:
Char | Ord | Translation | Code |
---|---|---|---|
p |
112 | 4 * 4 * 7 |
((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)) |
r |
114 | 4 * 4 * 7 + 2 |
((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...))--((...==...)--(...==...)) |
i |
105 | 3 * 5 * 7 |
((...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)) |
n |
110 | 3 ** 3 * 4 + 2 |
((...==...)--(...==...)--(...==...))**((...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...))--((...==...)--(...==...)) |
t |
116 | 4 ** 2 * 7 + 4 |
((...==...)--(...==...)--(...==...)--(...==...))**((...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...))--((...==...)--(...==...)--(...==...)--(...==...)) |
( |
40 | 2 * 4 * 5 |
((...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...)) |
' |
39 | 6 ** 2 + 3 |
((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...))**((...==...)--(...==...))--((...==...)--(...==...)--(...==...)) |
h |
104 | 4 * 5 ** 2 + 4 |
((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...))**((...==...)--(...==...))--((...==...)--(...==...)--(...==...)--(...==...)) |
e |
101 | 4 * 5 ** 2 + 1 |
((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...))**((...==...)--(...==...))--(...==...) |
l |
108 | 4 * 3 ** 3 |
((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...))**((...==...)--(...==...)--(...==...)) |
l |
108 | 4 * 3 ** 3 |
((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...))**((...==...)--(...==...)--(...==...)) |
o |
111 | 3 ** 3 * 4 + 3 |
((...==...)--(...==...)--(...==...))**((...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...))--((...==...)--(...==...)--(...==...)) |
, |
44 | 6 * 7 + 2 |
((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...))--((...==...)--(...==...)) |
|
32 | 2 ** 5 |
((...==...)--(...==...))**((...==...)--(...==...)--(...==...)--(...==...)--(...==...)) |
w |
119 | 4 ** 2 * 7 + 7 |
((...==...)--(...==...)--(...==...)--(...==...))**((...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...))--((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)) |
o |
111 | 3 ** 3 * 4 + 3 |
((...==...)--(...==...)--(...==...))**((...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...))--((...==...)--(...==...)--(...==...)) |
r |
114 | 4 * 4 * 7 + 2 |
((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...))--((...==...)--(...==...)) |
l |
108 | 4 * 3 ** 3 |
((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...))**((...==...)--(...==...)--(...==...)) |
d |
100 | 4 * 5 ** 2 |
((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...))**((...==...)--(...==...)) |
' |
39 | 6 ** 2 + 3 |
((...==...)--(...==...)--(...==...)--(...==...)--(...==...)--(...==...))**((...==...)--(...==...))--((...==...)--(...==...)--(...==...)) |
) |
41 | 2 * 4 * 5 + 1 |
((...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...))*((...==...)--(...==...)--(...==...)--(...==...)--(...==...))--(...==...) |