Home

An Exploration in Quine's

A quine is a self-replicating program. When run, it produces it's own source code. If you want to know more, see the wikipedia page on them.
In python, the most basic one is a "cheat" quine: read the file of the source code and print it

print(open(__file__).read())

But where is the fun in that? So after some deliberation I arrived at a proper quine: s= "{};print('s=',s.format(repr(s)))";print('s=',s.format(repr(s))) How does it work? Well, you store a means to display the program, and a string of it, and join them together! The trick is getting the string to include itself, which is thankfully quite easy in python.

As a bit of a joke you can get it to call itself, which of course results in a recursion depth exceeded error: s= "{};print('s=',s.format(repr(s))); exec(s)";print('s=',s.format(repr(s))); exec(s)

Where to from here?

Well, after some more thinking I realized that you can add a payload to a quine, where the self-execution is just a single case. Here's a comment that appears after a single run: s= "{};print('s=',s.format(repr(s))) # Payload ";print('s=',s.format(repr(s))) Now the quine doesn't execute to itself, it executes ... longer. But still to something that runs. Can we do something more useful than just putting in a comment? Yup, we can add in other commands: s= "{};print('s=',s.format(repr(s))); print('Hello') ";print('s=',s.format(repr(s))) But can we make it count how many times it has been run? Yup: s= "{};i={};print('s=',s.format(repr(s), i+1))";i=0;print('s=',s.format(repr(s), i+1)) And how about one that runs itself 100 times before stopping? s="""{} i={} print('s=',s.format(repr(s), i+1)) if i < 100: print('VALUE IS', i) exec(s.format(repr(s), i+1)) """ i=0 print('s=',s.format(repr(s), i+1));exec(s.format(repr(s), i+1)) Thank goodness you can get it to generate itself! This means I can separate the replication code from the payload code. Heck, you can even create a dictionary to store between-run variables in PAYLOAD = """ counter = g.get('counter', 0) g['counter'] = counter + 1 if counter > 100: g['continue'] = False """ s="""{{}} g={{}} print('s=',s.format(repr(s), g)) if g['continue']: {} exec(s.format(repr(s), g)) """.format(PAYLOAD) g={'continue':True} print('s=',s.format(repr(s), g));exec(s.format(repr(s), g)) At this point I realize I've written an extremely complex while loop. So what's the advantage? At Any time you can stop the program, take it's most recent output, save it, and then start it running later from that output. In other words, the entire program state is stored in it's output. Usefull? Probably not. Cool? Yup.