flushing of print statements ending with comma

Grebekel grebekel at gmail.com
Mon Dec 29 20:11:55 CET 2008


I have recently noticed that print statements ending with a comma are
not immediately flushed. This is evident when such statement is
executed before a very long operation (a big loop for instance).

Example:

<code>
print 'Take a walk, because this will take a while...',
i = 0
while i < 10**10:
    i += 1
print "we're done!"
</code>

Here the first string is not printed until the second print statement.
If the second print statement is removed then the string is not
printed until the end of the program. Apparently, python does not
flush the stdout buffer until it is ordered to do so either explicitly
or by a print statement not ending with a comma.

Using sys.std.flush after the print fixes this issue, but doing so
each time seems cumbersome and somewhat counterintuitive.

Is there some reasoning behind this behavior or is it a bug?

Python version 2.5.1



More information about the Python-list mailing list