back to index

Self-replicating Python code | Quine


Whisper Transcript | Transcript Only Page

00:00:00.000 | This Python code shown above, when executed,
00:00:03.560 | generates itself, shown below.
00:00:07.140 | This is what's called a quine.
00:00:09.980 | It is the kind of self-replicating system
00:00:12.580 | which is fascinating both philosophically and technically.
00:00:16.180 | The term quine was introduced by Douglas Hofstadter
00:00:18.740 | in his book, "Geitel, Escher, Bach."
00:00:21.300 | The idea of quines is echoes of self-replication
00:00:24.100 | in biological cells, where the cell contains data
00:00:27.380 | in the form of DNA and code in the form
00:00:30.180 | of biological processes which replicate that DNA.
00:00:33.580 | In the same way, the computational self-replication
00:00:36.060 | of a quine contains two elements.
00:00:39.140 | One is the code that performs the replication,
00:00:42.420 | and two is the data that contains all the raw materials
00:00:46.700 | for performing the replication process.
00:00:49.460 | So like I said, most quines have a data part and a code part.
00:00:53.100 | So let's look at this elegant Python quine.
00:00:56.900 | The data part is the string of the program,
00:00:59.540 | and the code part is the print statement
00:01:01.900 | that prints that program.
00:01:03.500 | So let's look at a basic example of the structure.
00:01:06.140 | The string is test%r, or %r is the repper function
00:01:11.140 | that returns a printable representation
00:01:13.300 | of the object that's passed.
00:01:15.620 | So when we execute this command of assigning the string
00:01:18.940 | to the variable s, and then print the string s,
00:01:23.620 | after passing itself to itself in a repper version
00:01:28.140 | of itself, and so the output of this code is test%r,
00:01:33.140 | with the test%r in quotes,
00:01:37.340 | which is the repper version of the argument.
00:01:40.760 | Now we can move back to our quine
00:01:42.760 | with the same exact structure of a string,
00:01:44.640 | but this time the string contains also the print statement
00:01:48.540 | with a double percent sign, which when printed,
00:01:50.980 | results in a single percent sign.
00:01:52.700 | So this code, once again, after you pass the repper version
00:01:56.500 | of the string to itself, results in a print
00:02:00.940 | of the entirety of the code.
00:02:03.380 | The recursive nature of this self-replicating piece of code
00:02:07.040 | has the beauty and the power that echoes
00:02:11.260 | the process of biological cell replication.
00:02:14.560 | I should briefly mention that there's a lot of ways
00:02:16.820 | of creating quines.
00:02:17.940 | The one I showed is the one I believe is the most elegant.
00:02:20.940 | There's also cheating ways of creating quines,
00:02:23.840 | which is, first of all, an empty program
00:02:26.180 | in many programming languages, including Python,
00:02:29.340 | is a quine itself.
00:02:30.880 | Nothingness, in a way, is forever
00:02:33.820 | effectively replicating itself.
00:02:35.560 | Also another obvious way is to put the code of the program
00:02:39.220 | into a file and then to print the contents of the file
00:02:42.180 | in which the code resides.
00:02:43.900 | Now this is actually officially considered to be cheating
00:02:47.540 | and not allowed to be in a quine.
00:02:49.300 | The quine has to be self-sustained.
00:02:51.780 | Now let me wrap up this video with the idea of an intron,
00:02:54.980 | which is code that you can inject within a quine
00:02:58.340 | that will replicate as part of the replication procedure,
00:03:01.980 | but does not actually contribute directly
00:03:04.200 | to the function of the quine.
00:03:05.260 | So it rides along for free, like a virus
00:03:08.620 | or a non-functional part of the genome in a DNA,
00:03:11.980 | without directly contributing
00:03:13.260 | to the replication of the code.
00:03:14.740 | Now this particular piece of code does more
00:03:18.180 | than just provide an intron.
00:03:19.700 | It takes the input from the user
00:03:22.140 | and adds that as an intron into the quine.
00:03:24.980 | Now the exact function is used here for brevity.
00:03:27.600 | So let's run this code.
00:03:28.980 | When we run it, it's waiting for the user input.
00:03:36.460 | If we just press enter, it generates the code itself,
00:03:40.740 | so it's a proper quine.
00:03:43.140 | If we run it again and provide input,
00:03:45.900 | like we live in a simulation,
00:03:49.700 | what you see is that the variable T
00:03:53.860 | now has a we live in a simulation string attached to it,
00:03:58.260 | and the entire program remains a proper quine.
00:04:02.300 | So if we copy it and run this program,
00:04:06.680 | it waits for the input.
00:04:09.540 | Now if we keep the same input,
00:04:11.620 | it generates the quine again successfully.
00:04:14.060 | Now we can run the same kind of quine
00:04:15.740 | with the we live in a simulation,
00:04:17.500 | and instead provide a new text, 42.
00:04:20.260 | The resulting output is a quine as well
00:04:22.220 | with a variable T now assigned the number 42.
00:04:27.220 | Now to me, quines and introns make me think
00:04:30.740 | about the source code of our own life here on Earth.
00:04:35.140 | And so I hope you may find this
00:04:36.660 | interesting and beautiful as I do.
00:04:38.900 | I may very well make a few more videos about quines,
00:04:42.940 | like multi-quines, polygon quines,
00:04:45.620 | quine relays, and other things that I find interesting
00:04:50.620 | in the space of computer science, mathematics, physics,
00:04:54.300 | artificial intelligence, machine learning,
00:04:56.760 | and even psychology, history, philosophy,
00:05:00.460 | biology, chemistry, and so on.
00:05:02.840 | So if you enjoy this kind of thing,
00:05:04.140 | consider subscribing, and remember,
00:05:06.300 | try to learn something new every day.
00:05:08.400 | (upbeat music)
00:05:10.980 | (upbeat music)
00:05:13.560 | (upbeat music)
00:05:16.140 | (upbeat music)
00:05:18.720 | (upbeat music)
00:05:21.300 | (Thanks for watching)