Python sys.exit() vs raise SystemExit

Which is more appropriate/efficient?

Page 1 of 1

4 Replies - 16923 Views - Last Post: 13 April 2008 - 06:26 PM Rate Topic: -----

#1 joshthejest  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 26-February 08

Python sys.exit() vs raise SystemExit

Posted 06 March 2008 - 11:30 PM

I am wondering which one of these commands is better to use in a python program. Is one more efficient than the other? Is one more correct to use than the other?

import sys
sys.exit()

or

raise SystemExit

Is This A Good Question/Topic? 0
  • +

Replies To: Python sys.exit() vs raise SystemExit

#2 not1975  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 26-December 06

Re: Python sys.exit() vs raise SystemExit

Posted 11 April 2008 - 06:28 AM

Hmm, that's interesting. I use sys.exit() to end a script, like in an if statement. I've never even heard of SystemExit. Do you know anything about it?

This post has been edited by not1975: 11 April 2008 - 06:29 AM

Was This Post Helpful? 0
  • +
  • -

#3 Nova Dragoon  Icon User is offline

  • The Innocent Shall Suffer, Big Time
  • member icon

Reputation: 36
  • View blog
  • Posts: 6,169
  • Joined: 16-August 01

Re: Python sys.exit() vs raise SystemExit

Posted 11 April 2008 - 07:27 AM

Python may be an exception to the rule, but in most compiled languages exceptions are very slow, taking time to halt setup handlers and dispatching to them. But since python is high level interpreted it may do exceptions at the same rate as function calls.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Python sys.exit() vs raise SystemExit

Posted 11 April 2008 - 09:17 AM

Agree with Nova, raising an event should be done only if need, generally when most other methods are impractical.

Strangely, sys.exit() is actually a wrapper for the event. From TFM: exit([arg]): Exit from Python. This is implemented by raising the SystemExit exception...

I'd note that forcing an exit is often a sign of a poorly design program. You should be able to fall to the bottom naturally. I don't know how Python handles exit condition values, which is the only time I'd use such a method. Basically, when you're already leaving.
Was This Post Helpful? 0
  • +
  • -

#5 dave_6_10  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 19-March 08

Re: Python sys.exit() vs raise SystemExit

Posted 13 April 2008 - 06:26 PM

In all actuality the real question becomes, which version of Python are you currently programming with that your using the exception exit with? version 2.2 of the python interpreter or earlier versions will likely have problems with this causing a slow down at compile time and also it is possible to see randomized exits in an otherwise error free run-time. Random exits are never a fun thing in your program. However, later versions of python such as python 25 for example will handle this much differently, and I am to assume that is what your using because of how you seem to like the exception exit. I would prefer to use the standard exit because it will tend to not confuse your fellow peers so much, (and keep in mind a programming language is not only needed to be interpreted by your computer but also by other people), and furthermore the changes that will be coming in Py3k may handle the translation of your programs in a complicated way.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1