shortest python quine?

后端 未结 10 1753
天涯浪人
天涯浪人 2020-12-23 19:55
_=\'_=%r;print _%%_\';print _%_

Is this the shortest possible python quine, or can it be done better? This one seems to improve on all the entrie

相关标签:
10条回答
  • 2020-12-23 20:47

    Even shorter:

    print(__file__[:-3])
    

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

    Edit: actually,

    print(__file__)
    

    named print(__file__) works too.

    0 讨论(0)
  • 2020-12-23 20:48

    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.

    0 讨论(0)
  • 2020-12-23 20:49

    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
    
    0 讨论(0)
  • 2020-12-23 20:49

    I would say:

    print open(__file__).read()
    

    Source

    0 讨论(0)
提交回复
热议问题