开发者

Shortest Python Quine?

Python 2.x (30 bytes):

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

Python 3.x (32 bytes开发者_如何转开发)

_='_=%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.


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


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


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.


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,)`)',)


Even shorter:

print(__file__[:-3])

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

Edit: actually,

print(__file__)

named print(__file__) works too.


Python 3.8

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


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


I would say:

print open(__file__).read()

Source


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)%%_)')%_)


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜