Skip to content

Instantly share code, notes, and snippets.

@aaronjgreenberg
Created August 24, 2012 16:16
Show Gist options
  • Save aaronjgreenberg/3452441 to your computer and use it in GitHub Desktop.
Save aaronjgreenberg/3452441 to your computer and use it in GitHub Desktop.
Writing Quines
#!/usr/bin/env python
# This program prints out its own source code
# ===========================================
import sys
def quine(filename):
f = open(filename, 'r')
for line in f.readlines():
print line.strip('\n')
if __name__ == '__main__':
quine(sys.argv[0])
#!/usr/bin/env python
# This program prints its own source code
# =======================================
def quine():
code = [
'#!/usr/bin/env python',
'# This program prints its own source code',
'# =======================================',
'',
'def quine():',
' code = [',
' ]',
' for i in range(6):',
' print code[i]',
' for i in range(len(code)):',
' print " " + chr(39) + code[i] + chr(39) + ","',
' for i in range(6, len(code)):',
' print code[i]',
'',
'quine()',
]
for i in range(6):
print code[i]
for i in range(len(code)):
print " " + chr(39) + code[i] + chr(39) + ","
for i in range(6, len(code)):
print code[i]
quine()
@aaronjgreenberg
Copy link
Author

A couple of quines I wrote, just for fun. The first is probably the easiest quine you can write that's not an empty file. The second is a more traditional quine, modeled after the example on Wikipedia.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment