且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我无法理解的 Python 打印行为

更新时间:2023-02-01 19:21:40

控制台终端是行缓冲的,这意味着控制台缓冲区将在找到换行符 \n 时刷新,这解释了您看到的行为现在,因为当您执行 print status 时,print 将自动打印一个新行字符,该字符将在 print 后刷新控制台缓冲区,但是当您执行 print status, 时,打印将不添加新行,因此打印后不会刷新缓冲区.

The console terminal is line buffered which mean that the console buffer will be flushed when it will found a new line character \n , which explain the behavior that you see now, because when you do print status print will print automatically a new line character which will flush the console buffer after the print , but when you do print status, the print will not add the new line so the buffer will not be flushed after the print.

这是一篇关于缓冲的好文章,希望它能给你更多的见识:)

Here is a good article about buffering hope it can give you more insight :)