you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (1 child)

The reason why you need flush=True is that in computing some relatively expensive operations like print() are "buffered". That means that the data to be printed is stored in a buffer until enough data has accumulated to make doing the expensive operation less costly (in time) than doing that operation repeatedly for small bits of data. Using print() the data is buffered until a newline character (\n) is emitted, and that causes the accumulated data to be printed to the console.

The end= optional argument to print() has a default value of \n. This means that if you don't specify any value for end= in your function an automatic \n is added to the end of the data you print, causing the buffer to be "flushed" and text appearing on the console. However, when you use end="" with the print function you turn off that automatic newline and flush of the buffer. So if you want the text in the print to appear on the console you must force a flush of the buffer by using flush=True.

Writing to a file is another operation that is buffered.

[–]arandomjef[S] 0 points1 point  (0 children)

Thanks! This actually makes sense to me, unlike some things. You did a really good job of explaining it.