41
votes
_='_=%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?)

11
Won't work with Python 3.x, by the way. - Michael Foukarakis
"can it be done better?" Although this question has definitively an answer it is hard to be answered (unless the answer is yes and you have a counterexample). How should someone know without testing ALL possible shorter programs? - Howard
@Howard: At some point, there aren't all that many of those... - Nemo
For the record, _='_=%r;print(_%%_)';print(_%_) works in python3. - Mechanical snail
I'd prefer to write it as r='r=%r;print r%%r';print r%r (for python2), though. - Mechanical snail

11 Answers

63
votes

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
20
votes

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).

14
votes

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.

11
votes

In a slightly non-literal approach, taking 'shortest' to mean short in terms of the number of statements as well as just the character count, I have one here that doesn't include any semicolons.

print(lambda x:x+str((x,)))('print(lambda x:x+str((x,)))',)

In my mind this contends, because it's all one function, whereas others are multiple. Does anyone have a shorter one like this?

Edit: User flornquake made the following improvement (backticks for repr() to replace str() and shave off 6 characters):

print(lambda x:x+`(x,)`)('print(lambda x:x+`(x,)`)',)
10
votes

Even shorter:

print(__file__[:-3])

And name the file print(__file__[:-3]).py (Source)

Edit: actually,

print(__file__)

named print(__file__) works too.

3
votes

Here is another similar to postylem's answer.

Python 3.6:

print((lambda s:s%s)('print((lambda s:s%%s)(%r))'))

Python 2.7:

print(lambda s:s%s)('print(lambda s:s%%s)(%r)')
3
votes

Python 3.8

exec(s:='print("exec(s:=%r)"%s)')
2
votes

I would say:

print open(__file__).read()

Source

0
votes

As of Python 3.8 I have a new quine! I'm quite proud of it because until now I have never created my own. I drew inspiration from _='_=%r;print(_%%_)';print(_%_), but made it into a single function (with only 2 more characters). It uses the new walrus operator.

print((_:='print((_:=%r)%%_)')%_)

0
votes

This one is least cryptic, cor is a.format(a) a="a={1}{0}{1};print(a.format(a,chr(34)))";print(a.format(a,chr(34)))

-25
votes

I am strictly against your solution.

The formatting prarameter % is definitively a too advanced high level language function. If such constructs are allowed, I would say, that import must be allowed as well. Then I can construct a shorter Quine by introducing some other high level language construct (which, BTW is much less powerful than the % function, so it is less advanced):

Here is a Unix shell script creating such a quine.py file and checking it really works:

echo 'import x' > quine.py
echo "print 'import x'" > x.py
python quine.py | cmp - quine.py; echo $?

outputs 0

Yes, that's cheating, like using %. Sorry.