shortest python quine?

假装没事ソ 提交于 2019-12-03 18:32:31

问题


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


回答1:


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



回答2:


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




回答3:


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.




回答4:


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



回答5:


Even shorter:

print(__file__[:-3])

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

Edit: actually,

print(__file__)

named print(__file__) works too.




回答6:


I would say:

print open(__file__).read()

Source




回答7:


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



回答8:


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.



来源:https://stackoverflow.com/questions/6223285/shortest-python-quine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!