Home>Article>Backend Development> How many pitfalls have people gone through when using Python? Avoid danger!
There is no doubt that theprint
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 noprint
Can't take the job.
But A-chan was almost fooled byprint
last time.
Initially, I wanted to add a progress display function to one of my command line gadgets, so I used thethreading
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'sprint
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 theend
parameter ofprint
The print result ofprint
.
So I got started and changed theprint("#")
call to print the progress toprint("#", 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 ofprint
.
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 ofprint
, so originally eachprint
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 thatsys.stdout.flush
can forcefully trigger the flushing of the standard output buffer, so inprint 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 forprint
, Its prototype is:
According to the description below, Python Whether the output ofprint
is buffered depends on two parameters:file
andflush
. Some types of
file
require buffering, such assys.stdout
; while others do not require buffering, such assys.stderr
.
For theflush
parameter, when its value isFalse
(default), whether to buffer depends onfile
; and when its value isTrue
, the buffer will be forced to be flushed.
Let’s modify theprint
call in the example call:
-uoption 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!