Welcome to Dream.In.Code
Getting Help is Easy!

Join 86,257 Programmers. There are 2,051 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

Python sys.exit() vs raise SystemExit

 
Reply to this topicStart new topic

Python sys.exit() vs raise SystemExit, Which is more appropriate/efficient?

joshthejest
post 6 Mar, 2008 - 11:30 PM
Post #1


New D.I.C Head

Group Icon
Joined: 26 Feb, 2008
Posts: 30



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?

CODE
import sys
sys.exit()

or

CODE
raise SystemExit
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


not1975
post 11 Apr, 2008 - 06:28 AM
Post #2


New D.I.C Head

*
Joined: 26 Dec, 2006
Posts: 43

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 Apr, 2008 - 06:29 AM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Nova Dragoon
post 11 Apr, 2008 - 07:27 AM
Post #3


The Innocent Shall Suffer, Big Time

Group Icon
Joined: 16 Aug, 2001
Posts: 5,754

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.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

baavgai
post 11 Apr, 2008 - 09:17 AM
Post #4


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,077

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.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

dave_6_10
post 13 Apr, 2008 - 06:26 PM
Post #5


New D.I.C Head

Group Icon
Joined: 19 Mar, 2008
Posts: 14

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.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/16/08 09:42AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month