Python quines
There is a specific type of program that, when run, produces its own source code as the ouput. They are called quines, named after the American philosopher Willard Van Orman Quine. I’ve seen many examples in C, and (although it seems like cheating), it’s awfully easy to do in Unix shell scripting:
#!/bin/cat
any text here
Doubting to Shuo has a post about writing self-replicating code (i.e. quines) using Python. He was able to use Python to write a quick quine script and notes:
For anyone that wants to try running that and see that it does indeed output its source, get a Python interpreter and try it out. My code above is not that impressive as quines go. I’m sure that in a powerful language like Python, somebody out there has already written a one-liner. What is impressive is that the language is powerful enough and convenient enough that writing a quine is easily doable for a complete beginner.
A very good point. If you look at quines in C or FORTRAN, they are definitely not something a beginner would be able to do.
And yes, there is a way to do it as a Python one-liner:
a='a=%r;print a%%a';print a%a
Cool stuff. If you want to check out more quines in other languages, I recommend taking a look at The Quine Page.
February 14th, 2006 at 1:12 pm
Is sooooooooo cheating that it belongs in the same category as “palindrome” programs that are written like this:
My favorite “cheat” is the ultimate “cheat”, which is both palindromic and a quine: