Các hàm thoát (), thoát (), sys.exit () và os._exit () có chức năng gần như giống như chúng tăng ngoại lệ SystemExit mà người phiên dịch Python thoát ra và không có dấu vết nào được in. Chúng ta có thể nắm bắt ngoại lệ để chặn lối thoát hiểm sớm và thực hiện các hoạt động dọn dẹp; Nếu chưa được thực hiện, thông dịch viên thoát ra như bình thường. & nbsp;
chức năng thoát () trong python
Hàm bỏ () chỉ hoạt động nếu mô -đun trang web được nhập nên không nên sử dụng trong mã sản xuất. Mã sản xuất có nghĩa là mã đang được sử dụng bởi đối tượng dự định trong tình huống thực tế. Hàm này chỉ nên được sử dụng trong phiên dịch. Nó làm tăng ngoại lệ hệ thống phía sau hậu trường. Nếu bạn in nó, nó sẽ đưa ra một thông điệp
Example:
Python3
for i in range(10____10
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit10 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit2 i 0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit40 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit4 0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit60 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit7
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit80 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit9An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 180
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit8An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 182
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit10 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit9An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 185
Output:
0 1 2 3 4 Use quit() or Ctrl-D (i.e. EOF) to exitexit () hàm trong python & nbsp;
Thoát () được xác định trong site.py và nó chỉ hoạt động nếu mô -đun trang web được nhập nên chỉ nên sử dụng trong trình thông dịch. Nó giống như một từ đồng nghĩa để thoát () để làm cho Python thân thiện hơn với người dùng. Nó cũng đưa ra một thông điệp khi được in: & nbsp;
Thí dụ
Python3
for i in range(10____10
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit10 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit2 i 0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit40 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit4 0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit60 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit7
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit80 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit9for2
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit8for4
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit10 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit9An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 185
Output:
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exitexit () hàm trong python & nbsp;
Thoát () được xác định trong site.py và nó chỉ hoạt động nếu mô -đun trang web được nhập nên chỉ nên sử dụng trong trình thông dịch. Nó giống như một từ đồng nghĩa để thoát () để làm cho Python thân thiện hơn với người dùng. Nó cũng đưa ra một thông điệp khi được in: & nbsp;zero is considered “successful termination”.
Note: A string can also be passed to the sys.exit() method.
Example:
Python3
Thí dụ
sys.exit ([arg]) sử dụng python
Không giống như thoát () và thoát (), sys.exit () được coi là tốt để được sử dụng trong mã sản xuất cho mô -đun SYS luôn có sẵn. Đối số tùy chọn ARG có thể là một số nguyên đưa ra lối ra hoặc một loại đối tượng khác. Nếu đó là một số nguyên, Zero được coi là chấm dứt thành công.
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit1i 8i 9in0
in10 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit7
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit10 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit9(in6in7
Output:
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 18for8 for9
i 00 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit4 i 2
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit2 i 4i 55____56 This method is normally used in the child process after os.fork() system call. The standard way to exit the process is sys.exit(n) method.
Python3
Os._Exit (n) trong Python
Phương thức Os._Exit () trong Python được sử dụng để thoát khỏi quy trình với trạng thái được chỉ định mà không gọi trình xử lý dọn dẹp, bộ đệm FLUSHING STDIO, v.v. & NBSP;
Lưu ý: Phương thức này thường được sử dụng trong quy trình con sau cuộc gọi hệ thống Os.Fork (). Cách tiêu chuẩn để thoát khỏi quá trình là phương pháp sys.exit (n).
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit10 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit9((0in7
for8 in9
range00 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit4 range2
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit2 range4range50 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit7
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit80 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit9(0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit020 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit03
‘
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit10 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit2 100101102
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit10 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit9(0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit140 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit15
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit10 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit9(0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit19in7
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit10 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit9(0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit24in7
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit10 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit27
Output:
In child process Process ID: 25491 Hello ! Geeks Child exiting.. In parent process Child's exit code: 00 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit81040 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit4 106101108Among the above four exit functions, sys.exit() is preferred mostly because the exit() and quit() functions cannot be used in production code while os._exit() is for special cases only when the immediate exit is required.