exit() or sys.exit()

Discussion in 'Python' started by Brendan, Jun 17, 2009.

  1. Brendan

    Brendan Guest

    What is the difference on exit() and sys.exit() when called in the
    main body of a script? From the command line they seem to have the
    same effect.

    Aside: Just used a python dictionary in which the keys were compiled
    regular expressions. Provided a very elegant solution. Have to love it.
    Brendan, Jun 17, 2009
    #1
    1. Advertising

  2. Brendan

    Tim Chase Guest

    Brendan wrote:
    > What is the difference on exit() and sys.exit() when called in the
    > main body of a script? From the command line they seem to have the
    > same effect.


    In Python <=2.4 you had to use sys.exit() because
    __builtins__.exit() griped:

    tchase@asgix:~$ python2.4
    Python 2.4.4 (#2, Apr 15 2008, 23:43:20)
    [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
    Type "help", "copyright", "credits" or "license" for more
    information.
    >>> type(exit)

    <type 'str'>
    >>> exit()

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: 'str' object is not callable

    In 2.5, it's an instance of site.Quitter which is callable,
    allowing it to behave like sys.exit() (from my observations,
    __builtins__.exit() and sys.exit() behave the same).

    I tend to use sys.exit() because I've still got code running on
    machines mired at 2.4

    -tkc
    Tim Chase, Jun 17, 2009
    #2
    1. Advertising

  3. Brendan

    Brendan Guest

    On Jun 17, 1:33 pm, Tim Chase <> wrote:
    > Brendan wrote:
    > > What is the difference on exit() and sys.exit() when called in the
    > > main body of a script? From the command line they seem to have the
    > > same effect.

    >
    > In Python <=2.4 you had to use sys.exit() because
    > __builtins__.exit() griped:
    >
    >    tchase@asgix:~$ python2.4
    >    Python 2.4.4 (#2, Apr 15 2008, 23:43:20)
    >    [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
    > Type "help", "copyright", "credits" or "license" for more
    > information.
    >    >>> type(exit)
    >    <type 'str'>
    >    >>> exit()
    >    Traceback (most recent call last):
    >      File "<stdin>", line 1, in ?
    >    TypeError: 'str' object is not callable
    >
    > In 2.5, it's an instance of site.Quitter which is callable,
    > allowing it to behave like sys.exit()  (from my observations,
    > __builtins__.exit() and sys.exit() behave the same).
    >
    > I tend to use sys.exit() because I've still got code running on
    > machines mired at 2.4
    >
    > -tkc


    Okay. Thanks.
    Brendan, Jun 17, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GinTon

    sys.stderr.write and sys.exit

    GinTon, Nov 23, 2006, in forum: Python
    Replies:
    5
    Views:
    566
    Dennis Lee Bieber
    Nov 24, 2006
  2. Yujo
    Replies:
    2
    Views:
    389
    Gabriel Genellina
    Apr 10, 2007
  3. Replies:
    3
    Views:
    285
    Gabriel Genellina
    Jul 13, 2007
  4. Harlin Seritt

    Sys.exit() does not fully exit

    Harlin Seritt, Feb 17, 2008, in forum: Python
    Replies:
    3
    Views:
    272
    Christian Heimes
    Feb 17, 2008
  5. chad
    Replies:
    14
    Views:
    556
    Arnaud Delobelle
    Oct 13, 2010
Loading...

Share This Page