#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Smile 2nd Shortest Quine in Python ever!


    I think I just thought of the shortest quine for python I think there can ever be! (except a blank program)

    make a program with this single line in it

    Code:
    print(__file__[:-3])

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

    and run the file!

    any shorter/cooler solutions out there that you know of?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,701
    Rep Power
    480
    I'm guessing it actually works. Where? How? When?
    Code:
    $ python3 print\(__file__[:-3]\).py 
    print(__file__[:-3])
    Traceback (most recent call last):
      File "print(__file__[:-3]).py", line 1, in <module>
        print(__file__[:-3]).py
    AttributeError: 'NoneType' object has no attribute 'py'
    It performed worse on python2.
    I use linux system.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by b49P23TIvg
    I'm guessing it actually works. Where? How? When?
    Code:
    $ python3 print\(__file__[:-3]\).py 
    print(__file__[:-3])
    Traceback (most recent call last):
      File "print(__file__[:-3]).py", line 1, in <module>
        print(__file__[:-3]).py
    AttributeError: 'NoneType' object has no attribute 'py'
    It performed worse on python2.
    I use linux system.
    you made a small mistake, leave out the .py part inside the code
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,592
    Rep Power
    4207
    If I leave out the .py, it won't work under linux.
    Code:
    mb@motorhead:~/python$ python print\(__file__\[\:-3\]\).py 
    print(__file__[:-3])
    Note that I have to run it with "python name_of_program.py" because .py is not a registered program type like on Windows, so the OS doesn't know what interpreter should be run to interpret .py files. The *nix way is to add this line as the first line in the file:
    Code:
    #!/usr/bin/python
    and then you can run it directly like this
    Code:
    ./print\(__file__\[\:-3\]\).py
    Your quine only works if you run it in Windows, with Windows Explorer set to hide file extensions .
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    76
    Rep Power
    2
    a quine is supposed to print out its source code, preferably in the least amount of code.

    i am running xubuntu, and the program runs in both python 3.2 and python 2.7 on my machine without any problems.

    the __file__ is the filename of the file, so __file__[:-3] should eliminate the last three characters from the filename.

    the filename should be set to:
    print(__file__[:-3]).py

    thereby when you print out __file__[:-3] it will print out its own sourcecode which is embedded in the filename.

    here is me running my code under linux:

    Code:
    leon@Leon:/$ python print\(__file__\[\:-3\]\).py 
    print(__file__[:-3])
    leon@Leon:/$ python2 print\(__file__\[\:-3\]\).py 
    print(__file__[:-3])
    leon@Leon:/$ python3 print\(__file__\[\:-3\]\).py 
    print(__file__[:-3])
    yes i have to run the file using the python interpreter, yes i know i could have written #!/usr/bin/python so that linux would know that it should execute this in the python interpreter, but that would ruin my quine, and I dont think its in the quine "rules" that i should have to run it with ./ instead of python2 in front.

    i have not tested this in windows though, but it sure works in linux, cant figure out why it shouldn't in yours.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,701
    Rep Power
    480
    Well dang, it does work!
    Good thinking, congratulations.

    Please post at rosettacode.org
    or I'll post it for you with credit to you, if you like.

    http://rosettacode.org/wiki/Category:Python

    http://www.nyx.net/~gthompso/quine.htm
    Last edited by b49P23TIvg; January 3rd, 2013 at 04:37 PM. Reason: fix wrong link
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    76
    Rep Power
    2
    thanks, i will it could be even shorter if i limit its use to python 2. i could skip the parantheses then
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    10
    Rep Power
    0
    What about print(__file__) on a file named print(__file__)?
    Code:
    $ cat print\(__file__\) 
    print(__file__)
    $ python print\(__file__\) 
    print(__file__)
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,701
    Rep Power
    480
    Nice. I included it for you onto rosetta code, here.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo