The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
2nd Shortest Quine in Python ever!
Discuss 2nd Shortest Quine in Python ever! in the Python Programming forum on Dev Shed. 2nd Shortest Quine in Python ever! Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 3rd, 2013, 02:07 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 2
Time spent in forums: 56 m 35 sec
Reputation Power: 0
|
|
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? 
|

January 3rd, 2013, 02:21 PM
|
 |
Contributing User
|
|
|
|
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!
|

January 3rd, 2013, 02:26 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 2
Time spent in forums: 56 m 35 sec
Reputation Power: 0
|
|
Quote: | 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
|

January 3rd, 2013, 03:14 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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:
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
|

January 3rd, 2013, 04:10 PM
|
|
Contributing User
|
|
Join Date: Jan 2013
Posts: 31
Time spent in forums: 9 h 18 m 30 sec
Reputation Power: 1
|
|
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.
|

January 3rd, 2013, 04:35 PM
|
 |
Contributing User
|
|
|
|
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
|

January 3rd, 2013, 04:40 PM
|
|
Contributing User
|
|
Join Date: Jan 2013
Posts: 31
Time spent in forums: 9 h 18 m 30 sec
Reputation Power: 1
|
|
thanks, i will  it could be even shorter if i limit its use to python 2. i could skip the parantheses then 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|