What is a quine?
A quine is a special computer program that takes no inputs and produces an exact copy of its source code when run. It’s often called a self-replicating program. Quines can be created in any powerful (Turing complete) programming language.
Crafting a quine
Typically, when crafting a Quine in any programming language, the approach involves incorporating two components within the program:
The code for carrying out the printing task
The data that encodes the program’s textual structure
The code employs the data to print the program itself because it precisely mirrors its text. The code simultaneously processes the data to print the textual representation of the data itself.
repr() in a Quine
In Python, the repr() function provides an unambiguous, “official” string representation of an object, showing all special characters, quotes, or escape sequences. This behavior is key in a quine because it allows the program to accurately reproduce itself as a string without reading the file directly.
Quine example
Here’s a simple example in Python:
quine = 'quine = {!r}\nprint(quine.format(quine))'print(quine.format(quine))
Code explanation
Let’s walk through and understand how the quine program works.
Line 1: The variable
quineholds a string representing the entire program's source code.Inside this string,
{!r}is a placeholder for therepr()function, signifying thatrepr(quine)will be used to convertquineto a representation that can include quotes and other formatting characters.repr()wrapsquinein quotes, preserving it exactly as a string, including characters like\n, quotes, and special characters, making it possible to print the program’s code exactly as it appears in source.
Line 2: When
quine.format(quine)is called,{!r}is replaced by the output ofrepr(quine). The finalprint(quine.format(quine))statement outputs this formatted string, which is now identical to the program's original source code.
Shortest possible quine
This example uses the same string formatting principles discussed above and uses it to craft a quine that’s as short as possible:
_='_=%r;print (_%%_)';print (_%_)
The %r is a format specifier in Python, which is used to get the representation of an object, which is a string that would recreate the object. Let us analyze the above line of code:
Code explanation
Line 1: The variable
_is assigned a string that contains a specific pattern:'_=%r;print (_%%_)'.Inside this string,
%ris a placeholder for therepr()function, which will return the string representation of the variable_.The
print(_%_)line is where the actual replication occurs.The
%operator here is used for string formatting. It takes the string in_and replaces%rwith the result ofrepr(_).The
%%inside the string is a special sequence in Python that represents a literal%character. So,print (_%%_)is effectively interpreted asprint (_ % _), which tells Python to format the string in_using itself as the argument.
Conclusion
A quine is a fascinating programming construct that showcases the ability of a program to reproduce its own source code without any external input. By using string formatting techniques, such as using the repr() function or format specifiers like {!r} and %r, quines can effectively replicate themselves while maintaining the integrity of their structure.
Free Resources