I wrote a Quine
Thursday, November 19th, 2009So I wrote my first Quine last night. A Quine is a program that, when run, outputs an exact copy of its source. It’s not immediately obvious in regard of how to accomplish that. At first it sounds pretty easy but then you get lost in recursion and your brain explodes.
So I wrote a Quine in Python. It’s not exactly short or impressive, but it works. I am pretty happy with myself, because I found out how to do it by myself, which was a very enlightening moment, makes you realize a bunch of things about compilers.
So here it is: (pretty vim-desert syntax colored version here: http://alex.underwares.org/quine.py.html)
#!/usr/bin/env python # Holy fucking shit I wrote a quine! # You wouldn't believe how baked I am. code ="#!/usr/bin/env python\n# Holy fucking shit I wrote a quine!\n# You wouldn't believe how baked I am.\n\ncode =\"__code__\"\n\ndef regurgitate(text):\n \"\"\"Hi, I regurgitate stuff, but escaped.\"\"\"\n text = text.replace(\"\\\\\",\"\\\\\\\\\")\n text = text.replace('\"', \"\\\\\\\"\")\n text = text.replace('\\n',\"\\\\n\")\n return text\n\ndef main():\n \"\"\"I seem to be the entry point, sir.\"\"\"\n print code.replace(\"__code__\", regurgitate(code), 1)\n\nif __name__ == \"__main__\":\n main()\n\n" def regurgitate(text): """Hi, I regurgitate stuff, but escaped.""" text = text.replace("\\","\\\\") text = text.replace('"', "\\\"") text = text.replace('\n',"\\n") return text def main(): """I seem to be the entry point, sir.""" print code.replace("__code__", regurgitate(code), 1) if __name__ == "__main__": main()

Against TCPA
