Home >Backend Development >Python Tutorial >How many pitfalls have people gone through when using Python? Avoid danger!
There is no doubt that the print
function is the most commonly used function in our daily life. Whether it is formatting output or printing intermediate variables for debugging, there is almost no print
Can't take the job.
But A-chan was almost fooled by print
last time.
Initially, I wanted to add a progress display function to one of my command line gadgets, so I used the threading
module to implement multi-threading. One thread is used to perform the actual logic, and the other thread is used to print the current progress.
Based on our
years of use using the command From the experience of lines, generally the printing progress is printed within the line, but Python's print
will print a newline character at the end by default, which is very unsightly.
Fortunately, print
also provides an interface to change the last character of printing, which can be changed by specifying the end
parameter of print
The print result of print
.
So I got started and changed the print("#")
call to print the progress to print("#", end="" )
.
Like this:
which becomes I thought, this change caused a big problem: the progress could not be printed in real time.
In other words, during the execution of the program, The # numbers printed one by one are no longer the obedient and cute
# numbers, but are output to the console at once after the entire program is executed.
It has grown up, has also become ugly.
Then what do I need you for?
At first, Ajiang thought there was a problem with multi-threading, so she foolishly looked for information everywhere to "support" her various speculations - thinking about it afterwards, it was so stupid that she still laughs when talking about it now.
The lesson from this incident is: Ten million Don't be self-righteous, but solve problems in a down-to-earth manner and treat every detail with an open mind.
Actually, the reason why we cannot see real-time output is because we changed the ending characters of print
.
In order to minimize I/O operations, Python has a mechanism: try to cache the output characters. When encountering the end of the string, a newline character, or forcing the buffer to be refreshed, the buffer will be cached at once. The contents of the area are output to the corresponding stream.
——What we changed is to remove the default newline character of print
, so originally each print
would trigger a buffer refresh. Now the buffer refresh cannot be triggered until the program ends.
Okay, now that we know what the problem is, we searched for information again. We heard that sys.stdout.flush
can forcefully trigger the flushing of the standard output buffer, so in print After
, sys.stdout.flush()
was added immediately.
Eh? Is it really good?
These are all knowledge points, jot them down. Come down, I have to take the test
Let’s check out the official documentation for print
, Its prototype is:
According to the description below, Python Whether the output of print
is buffered depends on two parameters: file
and flush
. Some types of
file
require buffering, such as sys.stdout
; while others do not require buffering, such as sys.stderr
.
For the flush
parameter, when its value is False
(default), whether to buffer depends on file
; and when its value is True
, the buffer will be forced to be flushed.
Let’s modify the print
call in the example call:
-u option when calling the program, which can also achieve real-time refresh of the buffer:
Related free learning recommendations:
The above is the detailed content of How many pitfalls have people gone through when using Python? Avoid danger!. For more information, please follow other related articles on the PHP Chinese website!