[Python-ideas] Pythonic buffering in Py3 print()
songofacandy at gmail.com
Tue Jan 10 06:52:39 CET 2012
What about adding flush=False keyword argument to print()?
On Tue, Jan 10, 2012 at 4:59 AM, Terry Reedy <tjreedy at udel.edu> wrote:
> On 1/9/2012 12:52 PM, Guido van Rossum wrote:
>> This has nothing to do with print or print(). There are no flush() calls
>> implied by print/print() calls, nor should there be(*). print/print()
>> simply calls sys.stdout.write() one or more times, the final call
>> usually being sys.stdout.write('\n') which will trigger a flush() if the
>> file is line-buffered -- but if you use a trailing comma (Python 2) or
>> end=... (Python 3) the last thing written is not '\n' and no flush()
>> will be triggered.
>> Whether writing \n flushes or not depends on the platform, the Python
>> version and implementation, and the environment (e.g. most platforms
>> automatically turn on line buffering for files known to be associated
>> with a terminal emulator or other interactive device, assuming there's
>> an easy way to tell).
>> (*) There are plenty of apps that construct their output using many
>> print/print() calls, and for which flushing after each call would be
>> quite unnecessary and potentially cause lots of redundant I/O
>> operations, ultimately slowing down the app.
> Thank you for the clarification. I noted on
> that there should be no code change
>> Apps that need flushing should call flush().
> and that saying something like this in the print doc entry is the only
> possible change.
> Terry Jan Reedy
> Python-ideas mailing list
> Python-ideas at python.org
INADA Naoki <songofacandy at gmail.com>
More information about the Python-ideas