0 and 1 are the exit codes.
exit(0) means a clean exit without any errors / problems
exit(1) means there was some issue / error / problem and that is why the program is exiting.
This is not Python specific and is pretty common. A non-zero exit code is treated as an abnormal exit, and at times, the error code indicates what the problem was. A zero error code means a successful exit.
This is useful for other programs, shell, caller etc. to know what happened with your program and proceed accordingly.
Answer from manojlds on Stack Overflow0 and 1 are the exit codes.
exit(0) means a clean exit without any errors / problems
exit(1) means there was some issue / error / problem and that is why the program is exiting.
This is not Python specific and is pretty common. A non-zero exit code is treated as an abnormal exit, and at times, the error code indicates what the problem was. A zero error code means a successful exit.
This is useful for other programs, shell, caller etc. to know what happened with your program and proceed accordingly.
This determines the exit status of the program when it finishes running (generally, 0 for success and 1 for error).
It is not unique to Python, and the exact effect depends on your operating system and how the program is called (though 99% of the time, if you're just running Python scripts, it doesn't matter).
Videos
Coming from perl, I've been doing:
if object.bad():
sys.exit(1)To exit a program if something goes wrong. It's occured to me that I should just raise an exception instead.
if object.bad():
raise Exception('object is bad')-
You might be able to skip importing sys.
-
It makes the code more reusable and portable, so calling code can catch the exception if they want.
-
It makes it a lot easier to print something to standard error.
If I'm just inserting something temporarily for debugging purposes, I will also sometimes do:
if object.bad():
assert False