Fedora Linux Support Community & Resources Center
  #1  
Old 17th March 2011, 07:39 PM
IHeart SymLinks Offline
Registered User
 
Join Date: Jan 2011
Posts: 128
linuxfedorafirefox
sys.exit()

I am reading http://learnpythonthehardway.com/ and in one of the scripts he does a from sys import exit

What is the difference between exit and sys.exit()

The script appears to work fine without the import, so why did he import it?
__________________
I♥SymLinks

Junior SysAdmin and Python Newbie
Fedora 15 64 Bit (Fedora 13 install upgraded to 14 with preupgrade, then 15 using --distro-sync)
KDE 4.6.5

Laptop: Fedora 13 upgraded to 15 via --distro-sync

Can someone pls help me with The wireless on my laptop?
Reply With Quote
  #2  
Old 31st March 2011, 01:15 PM
SGSeries2 Offline
Registered User
 
Join Date: Apr 2007
Posts: 43
windows_vistafirefox
Re: sys.exit()

Lol, it seems like you really are trying to learn the hard way. You've racked up as many posts here since the beginning of the year as I've posted since I joined. (But, then again, a lot of people have out-posted me, so I supposed that doesn't really say much.)

Anyways, to answer you question (and you've probably already figured this out).

When you use "from x import y" to import something, you're bringing y from module x into your local namespace, so when you say "from sys import exit", exit() is brought into your local namespace and can be called without using the module name first, like sys.exit(). So, the two both point to the same thing in the end.

I'd have to see the code as to why the import is there, but he likely made the import for later convenience, but maybe didn't use it (imported out of habit, perhaps), or testing never went down that path of execution where it would fail without the import. (Dynamic languages let you do that.) You don't absolutely have to call exit, but it is nice, and useful if you want your program to tell the caller something went wrong.

On importing (from a google search):
http://effbot.org/zone/import-confusion.htm
Reply With Quote
  #3  
Old 31st March 2011, 09:23 PM
IHeart SymLinks Offline
Registered User
 
Join Date: Jan 2011
Posts: 128
linuxfedorafirefox
Re: sys.exit()

Quote:
Originally Posted by SGSeries2 View Post
Lol, it seems like you really are trying to learn the hard way.
What do you mean by that?

Here is the code for one of the exercises:

Code:
from sys import exit

def gold_room():
    print "This room is full of gold.How much do you take?"

    next = raw_input("> ")
    if "0" in next or "1" in next:
        how_much = int(next)
    else:
        dead("Man, learn to type a number.")

    if how_much < 50:
        print "Nice, you're not greedy, you win!"
        exit(0)
    else:
        dead("You greedy bastard!")


def bear_room():
    print "There is a bear here."
    print "The bear has a bunch of honey."
    print "The fat bear is in front of another door."
    print "How are you going to move the bear?"
    bear_moved = False

    while True:
        next = raw_input("> ")

        if next == "take honey":
            dead("The bear looks at you then pimp slaps your face off.")
        elif next == "taunt bear" and not bear_moved:
            print "The bear has moved from the door. You can go through it now."
            bear_moved = True
        elif next == "taunt bear" and bear_moved:
            dead("The bear gets pissed off and chews your crotch off.")
        elif next == "open door" and bear_moved:
            gold_room()
        else:
            print "I got no idea what that means."


def cthulu_room():
    print "Here you see the great evil Cthulu."
    print "He, it, whatever stares at you and you go insane."
    print "Do you flee for your life or eat your head?"

    next = raw_input("> ")

    if "flee" in next:
        start()
    elif "head" in next:
        dead("Well that was tasty!")
    else:
        cthulu_room()


def dead(why):
    print why, "Good job!"
    exit(0)

def start():
    print "You are in a dark room."
    print "There is a door to your right and left."
    print "Which one do you take?"

    next = raw_input("> ")

    if next == "left":
        bear_room()
    elif next == "right":
        cthulu_room()
    else:
        dead("You stumble around the room until you starve.")


start()
He uses exit to end the program after the player wins the game or dies.

Funny thing is the program behaves the same way without the import. I like to use quit (same thing as exit?) when I have code I want to test. So for example if I have this file:
Code:
Here is code I already am working on
and I want to test this code
Code:
Code to test
Then I would simply add the code at the top and put in quit so it looks like:

Code:
Code to test





quit()
Here is code I already am working on
That way I can test the code without having to make a new file or delete the contents of my existing file and when I am done I just remove the test code and quit() statement.

But perhaps he imported it to get the reader used to the idea of importing. He didn't have to but made the reader do it anyways.
__________________
I♥SymLinks

Junior SysAdmin and Python Newbie
Fedora 15 64 Bit (Fedora 13 install upgraded to 14 with preupgrade, then 15 using --distro-sync)
KDE 4.6.5

Laptop: Fedora 13 upgraded to 15 via --distro-sync

Can someone pls help me with The wireless on my laptop?

Last edited by IHeart SymLinks; 31st March 2011 at 09:32 PM.
Reply With Quote
  #4  
Old 1st April 2011, 02:10 AM
SGSeries2 Offline
Registered User
 
Join Date: Apr 2007
Posts: 43
windows_vistaseamonkey
Re: sys.exit()

Quote:
Originally Posted by IHeart SymLinks View Post
What do you mean by that?
Oh, I was just playing with the website's name. Nothing more, really.

Quote:
Originally Posted by IHeart SymLinks View Post
Here is the code for one of the exercises:

... snip ...
That is interesting. I can't say I've used quit() or exit() that way (the automatic import versions, which are used if you comment out the import statement). Apparently exit and quit are constants that are automatically imported from the site module on initialization.

http://docs.python.org/library/constants.html#exit

And back in python 2.4, there were differences between the two (site & sys exit()s), but not anymore (I started using python post 2.5 release):
http://groups.google.com/group/comp....318b07030a048/

Like the docs say, the site module can be suppressed, and particularly the note on site's quit & exit (along with the other constants) may be of interest: "They are useful for the interactive interpreter shell and should not be used in programs." So I'd suggest sticking with sys.exit().

I know I learned something today.

Last edited by SGSeries2; 1st April 2011 at 02:21 AM.
Reply With Quote
Reply

Tags
sysexit

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How can i exit 'X' / gdm .... ??? RajivNair Using Fedora 4 11th August 2007 12:39 PM
Last Exit on FC6 austinjackson Using Fedora 1 27th January 2007 03:19 PM
I can't exit X medellin58 Using Fedora 2 21st March 2005 07:05 AM
Exit GUI TomWestcott Using Fedora 6 26th October 2004 07:04 PM


Current GMT-time: 15:56 (Friday, 24-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat