Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 3rd, 2013, 02:07 PM
leonnaley leonnaley is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 leonnaley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 35 sec
Reputation 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?

Reply With Quote
  #2  
Old January 3rd, 2013, 02:21 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,361 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 11 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old January 3rd, 2013, 02:26 PM
leonnaley leonnaley is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 leonnaley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old January 3rd, 2013, 03:14 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is online now
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,385 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 36 m 32 sec
Reputation Power: 4080
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

Reply With Quote
  #5  
Old January 3rd, 2013, 04:10 PM
leonnaley2 leonnaley2 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 31 leonnaley2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #6  
Old January 3rd, 2013, 04:35 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,361 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 11 sec
Reputation Power: 383
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

Reply With Quote
  #7  
Old January 3rd, 2013, 04:40 PM
leonnaley2 leonnaley2 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 31 leonnaley2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > 2nd Shortest Quine in Python ever!

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap