Examples and differences of using exit, return, sys. exit () in Python
This article mainly introduces the use cases and differences of exit, return, sys. exit () and so on in Python. This article is a summary of a practical project. For more information, see
There is a question: String identifier. modify the idcheck of Example 6-1. py script to detect the identifier with a length of one and identify the Python keyword. For the latter requirement, you can use the keyword module (especially the keyword. kelist) to help you.
My initial code is:
The Code is as follows:
#! /Usr/bin/env python
Import string
Import keyword
Import sys
# Get all keyword for python
# Keyword. kwlist
# ['And', 'as', 'assert ', 'Break',...]
KeyWords = keyword. kwlist
# Get all character for identifier
# String. letters => 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy'
# String. digits => '123'
CharForId = string. letters + "_"
NumForId = string. digits
IdInput = raw_input ("Input your words, please! ")
If idInput in keyWords:
Print "% s is keyword fot Python! "% IdInput
Else:
LenNum = len (idInput)
If (1 = lenNum ):
If (idInput in charForId and idInput! = "_"):
Print "% s is legal identifier for Python! "% IdInput
Else:
# It's just "_"
Print "% s isn' t legal identifier for Python! "% IdInput
Else:
If (idInput [0: 1] in charForId ):
Legalstring = charForId + numForId
For item in idInput [1:]:
If (item not in legalstring ):
Print "% s isn' t legal identifier for Python! "% IdInput
Sys. exit (0)
Print "% s is legal identifier for Python! 2 "% idInput
Else:
Print "% s isn' t legal identifier for Python! 3 "% idInput
After the code is complete, I test each branch. During the test, we must enter _ d4 % and other identifiers that contain invalid characters to perform the test. I initially thought that sys. exit (0) --- exit the script normally, sys. exit (1) the script is unexpectedly exited, but the actual situation is/9sys. exit (1), only the output return code is different ):
The Code is as follows:
If (item not in legalstring ):
Print "% s isn' t legal identifier for Python! "% IdInput
Sys. exit (0)
Input your words, please! _ D4 %
_ D4 % isn' t legal identifier for Python!
Traceback (most recent call last ):
File "E:/python/idcheck. py", line 37, in
Sys. exit (0)
SystemExit: 0
>>>
As we can see, this does not achieve the following output, so where is the problem? Because sys. exit () always throws a SystemExit exception.
The Code is as follows:
Input your words, please! _ D4 %
_ D4 % isn' t legal identifier for Python!
The Code is as follows:
#! /Usr/bin/env python
Import string
Import keyword
Import sys
Import traceback
Try:
# Get all keyword for python
# Keyword. kwlist
# ['And', 'as', 'assert ', 'Break',...]
KeyWords = keyword. kwlist
# Get all character for identifier
# String. letters => 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy'
# String. digits => '123'
CharForId = string. letters + "_"
NumForId = string. digits
IdInput = raw_input ("Input your words, please! ")
If idInput in keyWords:
Print "% s is keyword fot Python! "% IdInput
Else:
LenNum = len (idInput)
If (1 = lenNum ):
If (idInput in charForId and idInput! = "_"):
Print "% s is legal identifier for Python! "% IdInput
Else:
# It's just "_"
Print "% s isn' t legal identifier for Python! "% IdInput
Else:
If (idInput [0: 1] in charForId ):
Legalstring = charForId + numForId
For item in idInput [1:]:
If (item not in legalstring ):
Print "% s isn' t legal identifier for Python! "% IdInput
Sys. exit ()
Print "% s is legal identifier for Python! 2 "% idInput
Else:
Print "% s isn' t legal identifier for Python! 3 "% idInput
Failed t SystemExit:
Pass
Except t:
Traceback. print_exc ()
The above code gets the SystemExit exception thrown by sys. exit.
Return: return the return value of a function from the function when defining the function. Terminate the function execution.
Exit: In the following code, if sys. exit () is replaced with exit, exit only jumps out of the for Loop closest to it. print "% s is legal identifier for Python! 2 "% idInput statement will be output. Here, exit is similar to break. But in fact, break and exit are different.
The Code is as follows:
For item in idInput [1:]:
If (item not in legalstring ):
Print "% s isn' t legal identifier for Python! "% IdInput
Sys. exit ()
Print "% s is legal identifier for Python! 2 "% idInput