Different exit commands in Python
Hi everyone. today we are going to learn the various exit commands in Python. Python is an easy to use language but sometimes it may get a little complex. One such case is the use of various types of exit functions in python.
When we implement a program in Python, the code is executed sequentially until the end. But, there can be instances when we require the program to end earlier than expected. This is where the different exit functions come into play.
The various exit commands in Python
Here we are going to explain each function and when to use which function. So let us start.
This command is usually used by beginners/noobs, who are new to the language. It was developed as it is natural for people to write this keyword. This command is only to be used in your local machine. By no chance should it be applied to a code that is accessible to a person outside the local system, i.e code for clients, etc.
When invoked, it will enable the SystemExit exception.
Let us see with the help of an example.
for i in range(4, 14): if i == 11: print(quit) quit() print(i)
We start a loop from 4 to 14 and quit the program, when it reaches 11. The output will look like:
4 5 6 7 8 9 10 Use quit() or Ctrl-D (i.e. EOF) to exit Traceback (most recent call last): File "<string>", line 7, in <module> File "https://cdn.codespeedy.com/usr/lib/python3.7/_sitebuiltins.py", line 26, in __call__ raise SystemExit(code) SystemExit: None
We see that as soon as the if condition is satisfied, the program ends and we get the message:
“Use quit() or Ctrl-D (i.e. EOF) to exit”.
This command acts as an alias for the quit() command. Being a sibling to the quit() family and relying on the site module, this command also works locally and displays a message too when invoked. If there is some issue/error with the code, it will be an exit(1), else it’s exit(0).
for i in range(4,14): if i == 11: print(exit) exit() print(i)
4 5 6 7 8 9 10 Use exit() or Ctrl-D (i.e. EOF) to exit Traceback (most recent call last): File "<string>", line 6, in <module> File "https://cdn.codespeedy.com/usr/lib/python3.7/_sitebuiltins.py", line 26, in __call__ raise SystemExit(code) SystemExit: None
If there is a need to use a command to exit in live/ready code, then the sys.exit command has to be used. When the python code ends, it ensures less memory is being used after the program is run. This can also take a value as an argument. For example, sys.exit(0) for successful end or some output to display when invoked. Remember to import the sys package.
import sys day='tuesday' if day!='monday': sys.exit("it's not monday") else: print("it is monday")
We get output as:
Traceback (most recent call last): File "<string>", line 7, in <module>SystemExit: it's not monday>>>
This command is used to exit the program without calling any flushing stdio buffers, cleanup handlers, etc. This command is only used in special cases such as child-parent processes.
import os parent = os.fork() if parent > 0: print("\nMain parent ") info = os.waitpid(parent, 0) if os.WIFEXITED(info) : code = os.WEXITSTATUS(info) print("Child's exit code:", code) else : print("The child ") print(" ID:", os.getpid()) print("End of child process") os._exit(os.EX_OK)
The os.fork() is used to create a new child process.
Main parent The child ID: 1373 End of child process Child's exit code: 0 >>>
Do remember that the value of ID keeps on changing.
In general, they all are required to do the same thing, but, it also depends on what context they are invoked.
In case you are managing programs with production-ready specs, using sys.exit() and os._exit() makes sense. Else, local systems can use exit() or quit(). In general sys.exit() is most generally used.
I hope this was of good help.
Also read: How to exit from a loop in Python