Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shortest python quine?

_='_=%r;print _%%_';print _%_

Is this the shortest possible python quine, or can it be done better? This one seems to improve on all the entries on The Quine Page.

I'm not counting the trivial 'empty' program, and I'm not counting Terry Reedy's submission which is sus because of the double quotes (if that's allowed, is "hello world" a quine? or "'" for that matter?)

like image 828
wim Avatar asked Jun 03 '11 05:06

wim


3 Answers

I'm just going to leave this here (save as exceptionQuine.py):

    File "exceptionQuine.py", line 1
        File "exceptionQuine.py", line 1
        ^
IndentationError: unexpected indent
like image 145
Beurtschipper Avatar answered Sep 25 '22 01:09

Beurtschipper


Technically, the shortest Python quine is the empty file. Apart from this trivial case:

Since Python's print automatically appends a newline, the quine is actually _='_=%r;print _%%_';print _%_\n (where \n represents a single newline character in the file).

like image 23
Mechanical snail Avatar answered Sep 21 '22 01:09

Mechanical snail


Both

print open(__file__).read()

and anything involving import are not valid quines, because a quine by definition cannot take any input. Reading an external file is considered taking input, and thus a quine cannot read a file -- including itself.

For the record, technically speaking, the shortest possible quine in python is a blank file, but that is sort of cheating too.

like image 14
wintermute Avatar answered Sep 21 '22 01:09

wintermute