Skip to content

Instantly share code, notes, and snippets.

@CaedenPH
Created December 3, 2022 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CaedenPH/f653af49d5d432ab8c366bb3caf4a5a4 to your computer and use it in GitHub Desktop.
Save CaedenPH/f653af49d5d432ab8c366bb3caf4a5a4 to your computer and use it in GitHub Desktop.
Esoteric programming aproach

What is esoteric?

The definition in the oxford dictionary is:

adjective: esoteric

intended for or likely to be understood by only a small number of people with a specialized knowledge or interest.

"esoteric philosophical debates"

In other words, esoteric is doing something with the intent of confusing others.

What does esoteric mean in programming?

Writing esoteric code tends to be useless, with no other point than making your code as unreadable and unmaintainable as possible. However, esoteric code also pushes your knowledge of the programming language to the max and an be very useful for understanding the core components of that programming language.

Python

Esoteric code in python typically uses dunder methods, also known as magic methods (methods that are preceeded and followed by two underscores), that are called by the python internals. For example, when one calls the builtin str function, it calls the __str__ attribute on a class. This can be manipulated in such a way that instead of calling str, you call the object's __str__ method.

Quine

A quine is an example of an algorithm that is esoteric. A quine takes no input and produces a copy of its own source code as its only output.

This can be implemented in python as shown below:

print((lambda quine: quine % quine)("print((lambda quine: quine %% quine)(%r))"))

Read more about quines in the wikipedia - https://en.wikipedia.org/wiki/Quine_(computing)

My esoteric code

Below are some snippets of esoteric code that I have written.

>>> (_:=(type)((_X_:=lambda:0).__name__, (tuple)(), {f"{(_C__:=lambda:(''.__getattribute__)('join')([(chr)((2)*(3^5&10*(7+3))**2-(3%4))]*2))()}init{_C__()}":(lambda __:(4)if __.C_(__.__setattr__(_C__(),list(map(int,str(4*3-2)[::-1]))),7)else(None)), "C_": (lambda A,_c,B_: (eval((y:=lambda _Y:''.join(_Y))(((x:=y(list(map(lambda n: chr((2*n)+12+96), range(1,5)))))[1:3],chr(105),x[::3]))).__call__)(A.A(B_)[1])),"A":(lambda o,cc:(eval)(f"([o.__.append((sum)(o.__[(abs)(8%6)-4:])){(y:=lambda _Y:''.join(_Y))([chr((3*(n**2))+99)for n in range(1,3)]+['r'])}(_)in range(cc)],o.__)".__str__(),(locals)()))})());
[0, 1, 1, 2, 3, 5, 8, 13, 21]

image

>>> (print)(__builtins__.__getitem__('str')((__0__:=lambda: None)==((_0:=1),)).replace('False', '') if True is True else True is True, end='');_:__import__('typing').Callable[[str],str] = lambda m=None: True if m is False else False; ((print).__call__)(bool(''.join(list(map(lambda __m__: __m__.lower(), (str)((__:=(_,))[0](True if (___:=lambda e=None: _())() else (_)==(_)==[_])))))))
True

image

Conclusion

Overall, estoeric code is a way to enjoy and have fun with a programming language, and to push your programming language to the limit. And, if you need to put a backdoor in someones code, making it esoteric is the way to go!

Furthermore, some programming languages are invented for the sole purpose of being esoteric:

For further reading - https://en.wikipedia.org/wiki/Esoteric_programming_language

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment