[Python-3000] print() flushing problem.

Daniel Stutzbach daniel at stutzbachenterprises.com
Wed Nov 7 15:10:51 CET 2007

On Nov 7, 2007 6:59 AM, Wojciech Walczak
<wojtek.gminick.walczak at gmail.com> wrote:
> py3k's print() is not flushing when end is set to '' and all it has to
> print is one-character-long string:

print() shouldn't flush if there isn't a newline, unless the buffer is
full or the user calls flush().  However, in an interactive
interpreter in pre-3.0, Python will automatically add a newline.  Try
this in 2.5:

import time
print 'foo',

You'll see that "foo" appears *after* 10 seconds, not before.
However, in an interactive interpreter, Python will add a newline
after 'foo' ever with the comma there, and the newline causes a flush.
 Observe (2.5):

>>> print 'foo',
>>> print 'foo'

The same number of newlines are created with the comma and without,
which is different than the script behavior.

It sounds like P3K isn't doing this.

The patch you suggest would cause a flush after *every* write with
print, which would cause a very significant performance hit on
programs that do a lot of writing to standard output (especially if
standard output is a pipe).

Tangent: If standard output is a pipe and not a tty, is P3K smart
enough to switch to fully-buffered mode instead of line-buffered mode?

Daniel Stutzbach, Ph.D.             President, Stutzbach Enterprises LLC

More information about the Python-3000 mailing list