Ý nghĩa tuôn ra của python

print['\b' * len[positionStr], end='', flush=True]
6

print['\b' * len[positionStr], end='', flush=True]
7

print['\b' * len[positionStr], end='', flush=True]
8

print['\b' * len[positionStr], end='', flush=True]
9

print['\b' * len[positionStr], end='', flush=True]
0

print['\b' * len[positionStr], end='', flush=True]
1

print['\b' * len[positionStr], end='', flush=True]
2

print['\b' * len[positionStr], end='', flush=True]
3

print['\b' * len[positionStr], end='', flush=True]
4

print['\b' * len[positionStr], end='', flush=True]
5

print['\b' * len[positionStr], end='', flush=True]
70

print['\b' * len[positionStr], end='', flush=True]
71

print['\b' * len[positionStr], end='', flush=True]
72

Nếu bạn đã liên lạc với cộng đồng Python tương đối gần đây, có lẽ bạn đã nghe nói về

print['\b' * len[positionStr], end='', flush=True]
9.
print['\b' * len[positionStr], end='', flush=True]
9 mang màu sắc đến nhà ga. không gặp nhiều rắc rối

Hai dòng này hiển thị

print['\b' * len[positionStr], end='', flush=True]
1 ghi đè hàm
print['\b' * len[positionStr], end='', flush=True]
2 tích hợp của Python. Một điều khiến tôi chú ý là chuỗi tài liệu của
print['\b' * len[positionStr], end='', flush=True]
1, đặc biệt là dòng này

tuôn ra [bool, tùy chọn]. Không có tác dụng vì Giàu luôn tuôn ra đầu ra. Mặc định là sai

Tôi hiếm khi sử dụng thông số này. Nhưng tôi nhớ một nơi mà tôi đã nhìn thấy nó - tài liệu về chuột của

print['\b' * len[positionStr], end='', flush=True]
4 nơi Al Sweigart đưa ra ví dụ mã này hiển thị tọa độ con trỏ của bạn

    #! python3
    import pyautogui, sys
    print['Press Ctrl-C to quit.']
    try:
        while True:
            x, y = pyautogui.position[]
            positionStr = 'X: ' + str[x].rjust[4] + ' Y: ' + str[y].rjust[4]
            print[positionStr, end='']
            print['\b' * len[positionStr], end='', flush=True]
        except KeyboardInterrupt:
            print['\n']

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

Vì vậy, thực sự.

print['\b' * len[positionStr], end='', flush=True]
5 làm gì?

tuôn ra làm gì?

Không cần vòng vo thêm nữa, hãy tiếp tục và chạy tập lệnh này

print['\b' * len[positionStr], end='', flush=True]
5 không ảnh hưởng đến đầu ra. Những gì nó thay đổi là cách nó được in

Đây là những phát hiện của tôi

  • print['\b' * len[positionStr], end='', flush=True]
    
    0 [có, mặc định] sẽ đợi dòng hoàn thành trước khi in
  • print['\b' * len[positionStr], end='', flush=True]
    
    1 sẽ buộc thiết bị đầu cuối của chúng tôi in nó
  • print['\b' * len[positionStr], end='', flush=True]
    
    5 không thành vấn đề nếu
    print['\b' * len[positionStr], end='', flush=True]
    
    3

Bây giờ điều này đặt ra câu hỏi, tại sao?

in, hệ thống. thiết bị xuất chuẩn và PYTHONUNUNBUFERED

Dưới đây là câu đầu tiên từ tài liệu in của Python

print[*objects, sep=' ', end='\n', file=sys. stdout, flush=False]
In

print['\b' * len[positionStr], end='', flush=True]
6 ra luồng văn bản
print['\b' * len[positionStr], end='', flush=True]
7, phân tách bằng
print['\b' * len[positionStr], end='', flush=True]
8 và theo sau là
print['\b' * len[positionStr], end='', flush=True]
9.

Thành thật mà nói, khi lần đầu tiên tôi viết chương trình Hello World của mình bằng Python, tôi đã không xem qua tài liệu để thấy kinh ngạc khi triển khai nó. Bây giờ tôi đã nhìn thấy nó, tôi có thể suy ra rằng đầu ra của thiết bị đầu cuối là một "luồng văn bản" theo mặc định là

print['\b' * len[positionStr], end='', flush=True]
90

Từ các tài liệu

print['\b' * len[positionStr], end='', flush=True]
91 được sử dụng cho đầu ra của
print['\b' * len[positionStr], end='', flush=True]
92 và
print['\b' * len[positionStr], end='', flush=True]
93 và cho lời nhắc của
print['\b' * len[positionStr], end='', flush=True]
94

Các luồng này là các tệp văn bản thông thường giống như các tệp được trả về bởi hàm

print['\b' * len[positionStr], end='', flush=True]
95

Khi tương tác, luồng xuất chuẩn được đệm theo dòng. Mặt khác, nó được chặn vào bộ đệm giống như các tệp văn bản thông thường. Bạn có thể làm cho cả hai luồng không có bộ đệm bằng cách chuyển tùy chọn dòng lệnh

print['\b' * len[positionStr], end='', flush=True]
96 hoặc đặt biến môi trường
print['\b' * len[positionStr], end='', flush=True]
97

Rõ ràng là có một cái hố sâu để đào vào cái này. Nhiều lập trình viên có kinh nghiệm hơn có thể sẽ nhận ra

print['\b' * len[positionStr], end='', flush=True]
97 trong Dockerfiles của họ. Chúng tôi cũng có thể muốn biết sự khác biệt giữa bộ đệm dòng và bộ đệm khối. Cá nhân tôi đã biết về
print['\b' * len[positionStr], end='', flush=True]
99 hiện có

Nhưng hiện tại, chúng ta có thể thoải mái khi biết rằng đầu ra của thiết bị đầu cuối khá giống với một tệp văn bản thông thường được đọc từ đâu đó. Do đó, theo mặc định,

print['\b' * len[positionStr], end='', flush=True]
2 sẽ ưu tiên hoàn thành một dòng trước khi đưa ra kết quả.

Nhưng điều này ảnh hưởng đến

print['\b' * len[positionStr], end='', flush=True]
5 như thế nào?

  • Đặt PYTHONUNBUFFERED qua dòng lệnh. Tôi sử dụng Windows nên trong PowerShell, nó giống như
    print['\b' * len[positionStr], end='', flush=True]
    
    92. Điều này sẽ ghi đè
    print['\b' * len[positionStr], end='', flush=True]
    
    0. Nó cũng ghi đè lên
    print['\b' * len[positionStr], end='', flush=True]
    
    94
  • Mô-đun
    print['\b' * len[positionStr], end='', flush=True]
    
    95 [tôi. e. ,
    print['\b' * len[positionStr], end='', flush=True]
    
    96] KHÔNG ghi đè lên
    print['\b' * len[positionStr], end='', flush=True]
    
    0
  • Qua cờ python -u. Cờ
    print['\b' * len[positionStr], end='', flush=True]
    
    96 ghi đè
    print['\b' * len[positionStr], end='', flush=True]
    
    0 bất kể giá trị PYTHONUNBUFFERED

Giờ thì sao?

Chúng ta hãy quay trở lại mã chuột của Al Sweigart. Dòng này thực sự thú vị

print['\b' * len[positionStr], end='', flush=True]

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

cảnh báo spoiler. bạn có thể xóa đối số từ khóa

print['\b' * len[positionStr], end='', flush=True]
1. Ký tự
print['\b' * len[positionStr], end='', flush=True]
11 là backspace và đã đủ để xóa
print['\b' * len[positionStr], end='', flush=True]
12. Có lẽ nó chỉ hiển thị một số song song giữa mã này và mã Python 2 ngay bên dưới nó. Tuy nhiên, bạn có thể chơi với dòng này — thay đổi
print['\b' * len[positionStr], end='', flush=True]
9, sửa đổi
print['\b' * len[positionStr], end='', flush=True]
14, thậm chí bạn có thể thay đổi vòng lặp

Nhưng

print['\b' * len[positionStr], end='', flush=True]
5 sẽ làm gì ở đây?

Chủ Đề