How many pitfalls have people gone through when using Python? Avoid danger!

coldplay.xixi
Release: 2020-10-14 17:41:01
forward
2038 people have browsed it

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.

Where does the pit come from

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.

How many pitfalls have people gone through when using Python? Avoid danger!
Click and drag to move

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:

How many pitfalls have people gone through when using Python? Avoid danger!
Click and drag to move

which becomes I thought, this change caused a big problem: the progress could not be printed in real time.

How many pitfalls have people gone through when using Python? Avoid danger!
Click and drag to move

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.

How many pitfalls have people gone through when using Python? Avoid danger!
Click and drag to move

Then what do I need you for?

How many pitfalls have people gone through when using Python? Avoid danger!
Click and drag to move

What’s the problem?

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.

How many pitfalls have people gone through when using Python? Avoid danger!
Click and drag to move

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?

How many pitfalls have people gone through when using Python? Avoid danger!
Click and drag to move

These are all knowledge points, jot them down. Come down, I have to take the test

How many pitfalls have people gone through when using Python? Avoid danger!
Click and drag to move

Let’s check out the official documentation for print, Its prototype is:

How many pitfalls have people gone through when using Python? Avoid danger!
Click and drag to move

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:

How many pitfalls have people gone through when using Python? Avoid danger!
Click and drag Drag to move
How many pitfalls have people gone through when using Python? Avoid danger!
##Click and drag to move
You can also achieve progress Print in real time.

In addition, there is another way to add a

-u option when calling the program, which can also achieve real-time refresh of the buffer:

How many pitfalls have people gone through when using Python? Avoid danger!
Click and drag to move
How many pitfalls have people gone through when using Python? Avoid danger!
Click and drag to move Move
Of course, this method is not recommended. After all, no presets can be made for the users of the program.

Summary

This article is a record of Ajiang’s pitfalls. It records a strange problem in Python that few people will encounter.

In general, if you want to become a real Python programmer, it is not enough to simply master basic syntax and some tricks. You still need to have a certain understanding of Python itself.

After all, how can a swordsman travel around the world if he is not familiar with his own sword?

Related free learning recommendations:

python video tutorial

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!

Related labels:
source:juejin.im
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!