Các hàm quit[], exit[], sys. thoát [] và hệ điều hành. _exit[] có chức năng gần như giống nhau khi chúng tăng ngoại lệ SystemExit theo đó trình thông dịch Python thoát và không có dấu vết ngăn xếp nào được in. Chúng tôi có thể bắt ngoại lệ để chặn các lần thoát sớm và thực hiện các hoạt động dọn dẹp; .
hàm thoát [] trong Python
Hàm bỏ [] chỉ hoạt động nếu mô-đun trang được nhập, vì vậy không nên sử dụng hàm này 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ế. Chức năng này chỉ nên được sử dụng trong trình thông dịch. Nó làm tăng ngoại lệ SystemExit đằng sau hậu trường. Nếu bạn in nó, nó sẽ đưa ra một thông báo
Ví dụ.
Python3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit62
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit63
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit64
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit65
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit66
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit67
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit0
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit1
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit2
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit5
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3____07
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3______09
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit11
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit65
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit13
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit13
0 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 exit16
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit17
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit17
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit19
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit17
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit01
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit02
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit17
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit04
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3____201____207
đầu ra
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit7
hàm thoát [] trong Python
Lối ra [] được xác định trong trang web. py và nó chỉ hoạt động nếu mô-đun trang web được nhập, vì vậy nó chỉ được sử dụng trong trình thông dịch. Nó giống như một từ đồng nghĩa với từ bỏ [] để 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.
Ví dụ
Python3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit62
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit09
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit64
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit65
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit66
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit67
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit0
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit1
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit2
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit5
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3____07
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3____1632
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit11
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit65
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit13
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit13
0 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 exit16
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit17
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit641
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit17
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit01
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit644
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit17
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit646
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3____201____207
đầu ra
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit
hệ thống. thoát [[arg]] bằng Python
Không giống như thoát[] và thoát[], sys. exit[] được coi là tốt để 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à số nguyên, số 0 được coi là “kết thúc thành công”.
Lưu ý. Một chuỗi cũng có thể được chuyển đến sys. phương thức thoát [].
Ví dụ.
Python3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit62
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit651
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit652
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit653
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit654
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit13
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit656
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit657
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit11
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit659____1660
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit661
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3____1663
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3____1665
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit666
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit667
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit668
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit16
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3____201____00
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit673
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit674
đầu ra
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit1
hệ điều hành. _exit[n] trong Python
hệ điều hành. Phương thức _exit[] trong Python được sử dụng để thoát khỏi quy trình với trạng thái đã chỉ định mà không gọi trình xử lý dọn dẹp, xóa bộ đệm stdio, v.v.
Ghi chú. Phương pháp này thường được sử dụng trong tiến trình con sau os. lệnh gọi hệ thống fork[]. Cách tiêu chuẩn để thoát khỏi quy trình là sys. phương thức thoát [n]
Python3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit675
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit677
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit652
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit679
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit01
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit02
0 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 exit13
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit05
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit08
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit09
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit11
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit11____012
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit16
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3____201____00
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit18
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit674
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit21
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3____023
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit25
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit27
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit29
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3______031
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit13
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit33
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit12
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit674
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit17
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit39
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3____041
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3____043
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit45
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit48
_______03____050
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit52
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit17
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3____055
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit57
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3____111
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit60____061
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit62
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit17
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit64
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit13
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit66
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit61
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit68
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit17
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit01
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit0
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit72
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit73
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit668
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit16
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3____201____00
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit80
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit674
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3____201____00
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit85
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit86
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3____201____00
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit90
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit674
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3____201____00
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit95
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit674
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit17
_______03____099
_______03____1101
_______03____1103
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3____1105
đầu ra
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit0
Phần kết luận. Trong số bốn chức năng thoát trên, sys. exit[] được ưu tiên chủ yếu vì các hàm exit[] và thoát[] không thể được sử dụng trong mã sản xuất trong khi hệ điều hành. _exit[] chỉ dành cho các trường hợp đặc biệt khi cần thoát ngay lập tức