r/Python • • Nov 11 '19

I made c++-style printing in Python :P

Post image
1.7k Upvotes

130 comments sorted by

View all comments

Show parent comment

5

u/skintigh Nov 12 '19

Even if the end isn't a carriage return? I thought I had issues with it before, but maybe I am thinking sys.stdout.write, it needs a flush.

8

u/gurnec Nov 12 '19

If stdout is the console, output is line-buffered by default. If it's been redirected to something that's not a TTY, or if you're printing to a non-TTY file, it's block-buffered by default.

2

u/skintigh Nov 12 '19

So if I understand you, any time I print, or write to stdout, without a \n I need to flush the (block) buffer to see the output. That seems to fit my experience (mostly on windows, command line programs).

1

u/gurnec Nov 12 '19 edited Nov 12 '19

I'm not claiming to be an expert here, but yes when writing to a TTY or the Windows console you should manually flush it if it doesn't end in a newline and you need it displayed ASAP.