back to indexSelf-replicating Python code | Quine
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: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:30.180 |
of biological processes which replicate that DNA. 00:00:33.580 |
In the same way, the computational self-replication 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:49.460 |
So like I said, most quines have a data part and a code part. 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: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: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:52.700 |
So this code, once again, after you pass the repper version 00:02:03.380 |
The recursive nature of this self-replicating piece of code 00:02:14.560 |
I should briefly mention that there's a lot of ways 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:26.180 |
in many programming languages, including Python, 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:43.900 |
Now this is actually officially considered to be cheating 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:08.620 |
or a non-functional part of the genome in a DNA, 00:03:24.980 |
Now the exact function is used here for brevity. 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: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:22.220 |
with a variable T now assigned the number 42. 00:04:30.740 |
about the source code of our own life here on Earth. 00:04:38.900 |
I may very well make a few more videos about 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,