Self-replicating and growing code in Python

Cem Yusuf Aydoğdu
4 min readJul 26, 2020

--

I was watching this interesting video from Lex Fridman, about some self-replicating python code. I found this very interesting in a philosophical way.

So, I wanted to try it myself and played around with it. Here is the video:

Self-replicating Python code | Quine

In the video, he explains concepts like “quine” and “intron”. In short, quine is a program that outputs itself when executed, like this:

Try running this python code, it will output itself

In the video, Fridman explains intron as a part of quine, which does not contribute directly to the replication process. Similarly, biology defines introns as “parts of DNA or RNA which does not encode proteins”. In biological cells, these introns are excluded in the replication process.

So, this is an interactive intron code that he gave. It’s also a quine, and it takes an input from user.

If we run that code piece and give an empty input, it will just output itself. If we give it an input, it will create another quine which includes that input as the intron part:

Running intron.py with empty input
Running intron.py with an input

The text that we give rides along with the code. It doesn’t break the code, but also it doesn’t contribute to the function of the code.

This new quine has the exact specifications of the previous one. It will output itself when there is an empty input, it will create another quine with an input:

Trying the previous output

It’s already very interesting! Then I though, what happens if we give itself as the input? It outputs a longer version of itself, again with same functionalities:

Giving the code itself as the input. Highlighted part is the output

Longer version of the code, with same capabilities! This is not a surprise though, all it does is putting the user input to a string variable that is carried along by the code. In this case, the input is itself, so it stores itself as the intron part.

Now, what happens if we continue feeding itself as the input, and store the output in the same file? We can write a bash script to simulate this behavior:

It will just grow itself in size, without losing its purpose. After 3 runs of this script with bash run_intron.sh 3, the resulting intron.py will become this:

Resulting file still runs just like the original one. After just 3 runs it’s already unreadably long. In theory it can run for infinite times, but obviously this is not something we want.

Let’s find out the size of intron.py after each iteration. To try this, we could just append this line after counter increment i=$[$i+1] in the run_intron.sh:

echo “it:”$i “size:”$(ls -lh intron.py | awk ‘{print $5}’)

Here are the results. After 17th iteration, my system couldn’t handle it. 1844674407156206796 bytes is around 18 million TB. But, the resulting intron.py file is around 3.2 GB after the error. I guess it’s just an error of linux shell:

it:1 size:236
it:2 size:564
it:3 size:1,3K
it:4 size:2,7K
it:5 size:6,0K
it:6 size:14K
it:7 size:31K
it:8 size:75K
it:9 size:189K
it:10 size:492K
it:11 size:1,3M
it:12 size:3,7M
it:13 size:11M
it:14 size:30M
it:15 size:87M
it:16 size:256M
it:17 size:758M
run_intron.sh: xrealloc: cannot allocate 18446744071562067968 bytes

In a way, this interactive intron code acts like a biological cell. However, it doesn’t just replicate itself like a regular quine, it allocates more and more space in each iteration, while doing the same thing.

In another way, maybe it’s more like a cancer cell, or tumor? I know it’s not exactly like cancer, but it just reminds me of it. Maybe that’s why introns are excluded in replication in biological cells? I don’t know, biology is not my area. Better leave that question to experts.

Nevertheless, it’s fun to see a code that is self-replicating and growing by feeding itself. It could definitely serve as a strange benchmarking tool.

--

--