nfirvine.comwiki

FathersDayQuine

Filed in: Projects.FathersDayQuine · Modified on : Wed, 17 Jun 09

Herein find a Python quine, made especially for my D'Ad on Fathers' D'Ay 2009. (Okay, may have overdone the apostrophe thing a bit...)

#Happy Fathers' D'Ay, Pops!
s = "#Happy Fathers' D'Ay, Pops!\ns = %r\nprint s %% s"
print s % s

Here's how it works:

#Happy Fathers' D'Ay, Pops!

Just a comment.

s = "#Happy Fathers' D'Ay, Pops!\ns = %r\nprint s %% s"

The magic that makes it happen. s is assigned a string which is in fact a copy of the entire program. The %r is a string-formatting place holder.

print s % s

Here, we print s, but not before substituting the Python "repr" of s in for %r. In Python, calling the repr() function on an object returns a string that could be executed by Python to produce the object. Thus, calling eval(repr(o)) on an object that properly supports repr will return a copy of itself. (eval() executes a string as Python source code.)


Powered by PmWiki