Google Groupes n'accepte plus les nouveaux posts ni abonnements Usenet. Les contenus de l'historique resteront visibles.

exit() or sys.exit()

4 013 vues
Accéder directement au premier message non lu

Brendan

non lue,
17 juin 2009, 12:15:1717/06/2009
à
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.

Tim Chase

non lue,
17 juin 2009, 12:33:4417/06/2009
à Brendan,pytho...@python.org
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


Brendan

non lue,
17 juin 2009, 13:06:2017/06/2009
à

Okay. Thanks.

Sebastian Wiesner

non lue,
17 juin 2009, 17:41:5317/06/2009
à
<Brendan – Mittwoch, 17. Juni 2009 18:15>

> 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.

As of Python 2.5 there is no difference, however documentation [1] says
about exit() and quit():

> They are useful for the interactive interpreter shell and should not be
> used in programs.

[1] http://docs.python.org/library/constants.html#constants-added-by-the-
site-module

--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)

0 nouveau message