Self-replicating Python code | Quine

This is a short explanatory YouTube video (by LARRUZ, July 11, 2020) that demonstrates a self-replicating Python program (a quine). The speaker defines quines, explains their two-part structure (data string + code that prints repr of that string), walks through an elegant Python example using %r and double-% escaping, outlines illegitimate/cheating methods (empty program, reading source file), and introduces the idea of an "intron" — extra data or user input that rides along during replication without changing quine behavior. The video finishes by drawing analogies to biological replication and signalling possible future videos on related quine topics.
Chapters
This Python code shown above, when executed, generates itself shown below. This is what's called a quine. It is a kind of self replicating system, which is fascinating both philosophically and technically. The term quine was introduced by Douglas Hofstadter in his book Goethe Bach. The idea of quine has echoes of self replication in biological cells, where the cell contains data in the form of DNA and code in the form of biological processes which replicate that DNA. In the same way, the computational self replication of a quine contains two elements. One is the code that performs the replication, and two is the data that contains all the raw materials for performing the replication process. So like I said, most quines have a data part and a code part. So let's look at this elegant Python quine. The data part is the string of the program and the code part is the print statement that prints that program. So let's look at a basic example of the structure. The string is test percent r or percent r is the repr function that returns a printable representation of the object that's passed. So when we execute this command of assigning the string to the variable s and then print the string s after passing itself to itself in a repr version of itself, and so the output of this code is test test percent r with the test percent r in quotes, which is the repr version of the argument. Now we can move back to our quine with the same exact structure of a string, but this time the string contains also the print statement with a double percent sign, which when printed, results in a single percent sign. So this code, once again, after you pass the repr version of the string to itself, results in a print of the entirety of the code. The recursive nature of this self replicating piece of code has the beauty and the power that echoes the process of biological cell replication. I should briefly mention that there's a lot of ways of creating quines. The one I showed is the one I believe is the most elegant. There's also cheating ways of creating coins, which is first of all, an empty program in many programming languages, including Python, is a quine itself. Nothingness, in a way, is forever effectively replicating itself. Also, another obvious way is to put the code of the program into a file and then to print the contents of the file in which the code resides. Now, this is actually officially considered to be cheating and not allowed to be in a quine. The coin has to be self sustained. Now let me wrap up this video with the idea of an intron, which is code that you can inject within a quine that will replicate as part of the replication procedure, but does not actually contribute directly to the function of the quine. So it rides along for free like a virus or a non functional part of the genome in a DNA without directly contributing to the replication of the code. Now this particular piece of code does more then just provide an intron. It takes the input from the user and as that is an intron into the coin. Now the exact function is used here for brevity. So let's run this code. When we run it, it's waiting for the user input. If we just press enter, it generates the code itself. So it's a proper coin. If we run it again and provide input like we live in a simulation, what you see is that the variable T now has a we live in a simulation string attached to it and the entire program remains a proper coin. So if we copy it and run this program, it waits for the input. Now if we keep the same input, it generates the quine again successfully. Now we can run the same kind of quine with the welive in a simulation and instead provide a new text 42. The resulting output is a quine as well, with the variable T now assigned the number 42. Now to me, quines and introns make me think about the source code of our own life here on Earth. And so I hope you may find this interesting and beautiful. As I do, I may very well make a few more videos about quines like multiquines, polyglot quines, quine relays, and other things that I find interesting in the space of computer science, mathematics, physics, artificial intelligence, machine learning, and even psychology, history, philosophy, biology, chemistry and so on. So if you enjoy this kind of thing, consider subscribing. And remember, try to learn something new every day.










