Python Quine

A program that outputs itself.

In CS 6110 today, Dexter mentioned quines: programs that output their own source code. I quickly got sucked into a very fun internet rabbit-hole, including a collection of quines in different languages, a 128-language cyclical quine, a quine robust to mutations, and an HTML page that displays its own source.

I had to try it out for myself. Here’s what I came up with in Python 3:

from abc import ABC, abstractmethod
from string import ascii_lowercase


class AbstractQuine(ABC):

    imports = 'from abc import ABC, abstractmethod\n' \
              'from string import ascii_lowercase'

    @abstractmethod
    def print_code(self):
        ...


class Quine(AbstractQuine):

    def print_code(self):
        unmap = lambda x: x[0].replace('d', 'd\\n') + f' \\\n{s}{s}{s}  ' + x[1]
        s, n, q, t, r, b = '    ', '\n', '\'', '~', 'r', '\\'

        body = r'{self.imports}\n\n\nclass {self.__class__.__base__.__name__}~' \
               r'({ascii_lowercase[:3].upper()}):\n\n~' \
               r'{s}imports = {unmap(list(map(repr, self.imports.split(n))))}\n\n~' \
               r'{s}@abstractmethod\n{s}def print_code(self):\n{s}{s}...\n\n\n~' \
               r'class Quine({self.__class__.__base__.__name__}):\n\n~' \
               r'{s}def print_code(self):\n~' \
               r'{s}{s}unmap = lambda x: x[0].replace({q}d{q}, {q}d\\\\n{q})~' \
               r' + f{q} \\\\\\n{{s}}{{s}}{{s}}  {q} + x[1]\n~' \
               r'{s * 2}s, n, q, t, r, b = {q}    {q}, {repr(n)}, \'\\\'\', ~' \
               r'{q}{t}{q}, {q}r{q}, {q}{b}{b}{q}\n\n~' \
               r'{s * 2}body = {r}{q}{body.replace(t, t + q + s[1] + b + n + ~' \
               r'3 * s + s[:3] + r + q)}{q}\n\n~' \
               r'{s * 2}exec(f{q}print(f\"{{body.replace(t, \"\")}}\"){q})\n\n\n~' \
               r'{self.__class__.__name__}().print_code()'

        exec(f'print(f"{body.replace(t, "")}")')


Quine().print_code()

Some people might not like using exec, but I think it’s fair game. To verify that this is a quine, you can save the above code in quine.py and run python3 quine.py | diff quine.py - (make sure to have one newline after Quine().print_code()). Of course, much shorter quines are possible in Python, notably:

c='c=%r;print(c%%c)';print(c%c)

from Wikipedia, but it was fun to make something a bit longer work.

Kiran Tomlinson
Kiran Tomlinson
Senior Researcher

I’m a recent PhD graduate from Cornell University, starting this fall as a Senior Researcher at Microsoft Research.