Self-replicating Code in Python
February 13th, 2006 by MarkHave you ever tried to write a program that outputs itself when you run it? While I was eating at Wahoo’s Fish Tacos with Mike and Matt a couple of weeks ago, Matt brought up the topic. Being the guy that he is, he whipped out a napkin and proceeded to write an example of a self replicating C program.
I guess it’s been a fairly well-known topic for a while, after being popularized by Ken Thompson’s speech twenty years ago. These self-replicating programs have a special name- they’re called quines. It’s an interesting, and surprisingly difficult task to make a quine, especially in low-level languages like C. I should know, because I had to beat my head against the problem for a bit before it came to me.
The interesting thing, though, is it’s not that hard to write a quine in Python. Due to some of the interesting essays by Paul Graham that I’ve been reading, I’ve started learning some Python over the last week. Despite knowing only a very small amount of the language, I was already to write this quine tonight:
x = ['print "x =", x', 'for m in x: print m']
print "x =", x
for m in x: print m
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. If I were using FORTRAN, for instance, I’d have to do a fair amount of brushing up before I’d even have a hope. I’ve never been much of a fan of Java. I’ve always been a Perl guy, except for graphics engines (which I did in C). Paul Graham’s on to something with Python, though. It’s surprisingly easy to get up to speed with it, and it’s much more powerful than I expected, too.
Update: This page includes many Python quines, including one-liners and one that’s logically equivalent to mine.
Update 2: Come on all you geeks out there, post your quines as comments here! Matt, you could make one in Javascript, right? I mean… you’re not a wuss, right? Ducky, how about flexing those MATLAB trained mental muscles of yours? Mike W, you said you were getting pretty into PHP. .. post me a quine!
:
February 14th, 2006 at 7:16 am
[...] 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. [...]
February 21st, 2006 at 12:05 pm
For the curious, here’s basically the version that I wrote on the napkin:
void main(void) { char *s=”void main(void) { char *x=%c%s%c; printf(s, 34, s, 34); }”; printf(s, 34, s, 34); }
(This is a C program that prints out it’s source code)
On some systems, it will compile with warnings because ‘printf’ is not defined before use, and the ‘main’ function does not return type ‘int’. Even so, it will compile with gcc.
I’ll skip the JavaScript version for now, but I might put up one for Ruby. Stay tuned…
February 21st, 2006 at 12:08 pm
Ah! I new somebody out there would contribute to this thread! Good to hear from you, Matt.
February 21st, 2006 at 2:39 pm
Okay, here it is for Ruby:
s=’s=%c%s%c; printf s,39,s,39′; printf s,39,s,39
Interestingly, it’s identical to the perl solution, except the variable ’s’ doesn’t need a ‘$’ prefix. (Ruby borrowed a fair bit from perl and a little from the C standard library).
I’ve recently become interested in the concept of the ‘perfect’ programming language. Along these lines, Slashdot had a very provocative article reviewing the book Java.Beyond Java.
This book mentions four programming languages that are the best candidates for replacing Java: Ruby, Python, Groovy, and .NET. Secondary contenders are perl (too messy), Smalltalk (wasn’t java), PHP (too close to HTML), and LISP (not accessible). The author really liks Ruby as the first choice, but didn’t like the fact that Ruby doesn’t have a good Java VM implementation. His second favorite was Python.
Another thing that piqued my interest in Ruby was that the Pragmatic Programmers have written a book on ruby. I went out and bought this book and have been busy reading it ever since. It really does look a lot better than many languages, but I still can’t call it the ‘perfect’ language. Even so, it seems like a step in the right direction.
I’ll have to try some Python next, though. JavaScript may have to wait a while. There’s also another language named ‘D’ that might have some promise…
February 21st, 2006 at 8:34 pm
What does he mean by “.NET”? C/C++, Java, C#, Perl, VB and a host of other languages can all be used in .NET development.
February 23rd, 2006 at 12:05 pm
Same basic idea in perl:
$s = ‘$s = %c%s%c; printf $s, 39, $s, 39;’; printf $s, 39, $s, 39;
April 28th, 2006 at 10:09 pm
I got one in c#
look there:
http://peitor.blogspot.com/2006/04/quine-in-csharp-c_28.html
but there are other good ones around
google for: quine c# main