Hướng dẫn print and exit in python - in và thoát trong python

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 exit
1
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
2 i
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
4
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
4
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
6
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
7

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
8
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
9
An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
0

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
8
An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
2

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
1
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
9
An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
5

Output:

0
1
2
3
4
Use quit() or Ctrl-D (i.e. EOF) to exit

exit () 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 exit
1
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
2 i
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
4
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
4
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
6
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
7

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
8
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
9for2

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
8for4

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
1
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
9
An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
5

Output:

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit

exit () 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 exit
1i 8i 9in0

in1

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
7

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
1
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
9(in6in7

Output:

An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18

for8 for9

i 0

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
4 i 2

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
2 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 exit
1
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
9((0in7

for8 in9

range0

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
4 range2

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
2 range4range5
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
7

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
8
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
9(
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
02
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
03

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
1
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
2 100101102

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
1
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
9(
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
14
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
15

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
1
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
9(
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
19in7

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
1
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
9(
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
24in7

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
1
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
27

Output:

In child process
Process ID: 25491
Hello ! Geeks
Child exiting..

In parent process
Child's exit code: 0

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
8104
0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit
4 106101108
Among 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.