Alexander Bass

Composing Python built-ins to make text

A friend of mine sent me an ominous message about programming:

Don’t run this in the Python repl

print(chr(sum(range(ord(min(str(not())))))))

I figured that somehow this piece of code would crash the interpreter or something but, after disregarding their suggestion, I was delighted to see it print

How does this string of incomprehensibly nested function compositions create anything at all?

not(). This is not a function, but rather Python’s not operator on an empty tuple. Empty tuples are considered to be false in a boolean sense, so the output of this is the negation, True

str(True). Cast the boolean to a string value, resulting in "True".

min("True"). This one is a bit more complex. min searches through each character in the string, and returns the one with the smallest Unicode Codepoint value. Capital letters come before lowercase letters, thus the result is "T"

ord("T") is fairly simple, it takes a character and returns the codepoint value as an integer, which happens to be 84.

sum(range(84)) I’ve lumped these two together because they work as a unit. This finds the sum of all integers between 0 and 83 (inclusive), which is 3486.

chr(3486) takes an integer codepoint and converts it back into a character which happens to be .

Hello World

The example above got me thinking: just what can be made by composing built-in functions?

Anyhow, I thought this idea was cool enough that I made a hello world with it.

print(
    chr(max(range(max(range(ord(max(str(complex(int()))))))))),
    next(reversed(str(bool()))),
    chr(max(range(max(range(max(range(ord(max(oct(int())))))))))),
    chr(max(range(max(range(max(range(ord(max(oct(int())))))))))),
    max(oct(int())),
    min(str(slice(int()))),
    chr(max(range(ord(max(hex(int())))))),
    max(oct(int())),
    max(str(range(int()))),
    chr(max(range(max(range(max(range(ord(max(oct(int())))))))))),
    chr(max(range(max(range(max(range(max(range(max(range(max(range(ord(max(str(complex(int())))))))))))))))))
)

I won’t be explaining how this one works.

So if you want to expand upon this, here are the “rules” for creating one of these expressions:

Here’s a table of the functions which should follow those rules (depending on how you use them)

bool ascii sum
bytearray bin any
complex chr all
float hex callable
dict oct enumerate
frozenset repr memoryview
bytes format iter
set round next
list ord property
tuple abs range
str len reversed
object max slice
int min sorted
type

Hello World 2

This all got me thinking: this would be a cool way to obfuscate code. In the previous example, the string “Hello World” was constructed, but what about a program which prints hello world?

So I wrote a simple python program

print("Hello World!")

Then, each character is transformed into the wacky composed function form and concatenated together. Finally, the resulting string is wrapped into an exec expression to run the code.

exec(
chr(max(range(max(range(ord(max(str(range(int()))))))))) +\
max(str(range(int()))) +\
chr(max(range(ord(max(str(complex(int()))))))) +\
chr(max(range(ord(max(oct(int())))))) +\
max(str(type(int()))) +\
min(str(set())) +\
chr(max(range(max(range(sum(range(len(bin(ord(max(str(not())))))))))))) +\
chr(max(range(max(range(max(range(max(range(max(range(max(range(sum(range(len(str(type(int())))))))))))))))))) +\
next(reversed(str(bool()))) +\
chr(max(range(max(range(max(range(ord(max(oct(int())))))))))) +\
chr(max(range(max(range(max(range(ord(max(oct(int())))))))))) +\
max(oct(int())) +\
min(str(type(int()))) +\
chr(max(range(max(range(max(range(max(range(ord(min(str(list())))))))))))) +\
max(oct(int())) +\
max(str(range(int()))) +\
chr(max(range(max(range(max(range(ord(max(oct(int())))))))))) +\
chr(max(range(ord(next(reversed(str(bool()))))))) +\
chr(len(str(slice(sum(range(sum(range(sum(range(ord(min(str(not()))))))))))))) +\
chr(max(range(max(range(sum(range(len(bin(ord(max(str(not())))))))))))) +\
next(reversed(str(set()))))