Stopping Execution

J

James Colannino

Hey everyone. I remember from my C programming that I can either use
the exit() or return() functions to end execution of the main code
block. My question is, is there a way for me to do this in Python? I
know there has to be, but I can't for the life of me figure out what it
is. The reason I ask is that I need to conditionally end a script I'm
writing at various places. Thanks in advance.

James

--
My blog: http://www.crazydrclaw.com/
My homepage: http://james.colannino.org/

"A well regulated militia being necessary to the security of a free
state, THE RIGHT of the people to keep and bear arms SHALL NOT BE
INFRINGED." --United States Constitution, Second Ammendment
 
F

Fredrik Lundh

import sys
sys.exit

$ more test.py
import sys
print "should get here"
sys.exit
print "should never get here"

$ python test.py
should get here
should never get here

</F>
 
S

Steve Holden

Fredrik said:
$ more test.py
import sys
print "should get here"
sys.exit
print "should never get here"

$ python test.py
should get here
should never get here
Which is Fredrik's way of telling you you need to *call* sys.exit and
not just reference it (works in Perl, which assumes you want to call it,
but in Python "explicit is better than implicit").

$ more test.py
import sys
print "should get here"
sys.exit()
print "should never get here"

$ python test.py
should get here

regards
Steve
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top