Cyclic Quine

2017-08-12, post № 177

Programming, Python, Quine, #self-referential

A classic quine is a program which outputs its own source code.
At first, such a program’s existence seems weird if not impossible, as it has to be so self-referential that it knows about itself everything, including how to know about itself. However, writing quines is possible, if not [1] trivial.

A cyclic quine therefore is a program which outputs source code which differs from its own source code, yet outputs the original source code when run (the cycle length could be greater than one). So when running source codes 𝛹 and 𝛷, they output source codes 𝛷 and 𝛹.

Therefore, when one saves the first program as q0.py and the second as q1.py, one can create both source codes from one another (the following bash commands [2] will not change the files’ contents).

$ python q0.py > q1.py
$ python q1.py > q0.py
$ python q0.py | python > q0.py
$ python q0.py | python | python | python | python > q1.py
Source code: cyclic-quine_q0.py
Source code: cyclic-quine_q1.py


  1. [2020-08-05] In the sense of “even though not”, exaggerating the task’s difficulty.
  2. [2020-08-05] Any shell will suffice; one nowadays possibly needs to specify python2.