[Python-ideas] Pythonic buffering in Py3 print()

anatoly techtonik techtonik at gmail.com
Tue Jan 10 14:27:24 CET 2012


I see that Python 3 behavior is consistent on Windows too meaning it
doesn't output anything on the screen when end=''

It seems that basic function print() that was created to just "print a
string" got over-engineered breaking unix principle of "doing one thing".
It reminds me of subprocess.Popen which was also designed to just "run a
process". I would keep it as simple as possible for printing stuff to the
screen without any implicit buffering. You called print() - you get the
output. KISS. If people need more control with output buffering - provide a
pointer to sys.stdout with all gory details about streams etc.

To get back on topic, let me give a definition. To me "pythonic" means
"intuitive" in the first place. And "intuitive" means "up to user
expectations". What does user expect from "print()" function? Writing to a
file? I don't think so. Output to printer? Maybe. Printing to the screen?
Definitely.

"intuitive" also means that users won't have to consult user documentation
for every basic thing it needs, especially for such basic thing such as
getting some stuff printed to the screen output - the stuff that is
essential in the coding process. It is already less convenient in Py3k,
because you need to type extra Shift-9 and Shift-0 in the process, and it
risks to become completely weird:

  print('calling external process... ', end='')
  sys.output.flush()
  ...
  print('done')


So, py3k print() is not pythonic. To make it pythonic, the output should be
immediately available after you call it. If you need to buffer output - do
this explicitly, because print() doesn't guarantee that it will be buffered
anyway. If you care about performance - use sys.stdout directly and tune it
explicitly.


On Mon, Jan 9, 2012 at 4:25 PM, Devin Jeanpierre <jeanpierreda at gmail.com>wrote:

> > This produces one dot every second with Python 2.7.2 on Windows.
>
> Weird. Must be a windows thing.
>
> > I assume you use Linux. Do you agree that for cross-platform language
> some
> > behavior should be made consistent?
>
> Yes. I guess that's why they changed the behavior of print on Windows
> in Python 3.
>
> -- Devin
>
> On Mon, Jan 9, 2012 at 7:21 AM, anatoly techtonik <techtonik at gmail.com>
> wrote:
> > On Mon, Jan 9, 2012 at 1:53 PM, Devin Jeanpierre <jeanpierreda at gmail.com
> >
> > wrote:
> >>
> >> > In Python 2 "print 'something', statement calls were unbuffered and
> >> > immediately appeared on screen.
> >>
> >> No, they weren't.
> >
> >
> >  --- cut py2print.py ---
> > from time import sleep
> >
> > while 1:
> >   sleep(1)
> >   print ".",
> > --- cut ---
> >
> > This produces one dot every second with Python 2.7.2 on Windows.
> >
> >> Python doesn't do any extra buffering or flushing. Usually your
> >> terminal emulator line-buffers stdout. If you don't print a newline
> >> (in Python 2 OR 3) then it doesn't show up until you either flush
> >> (sys.stdout.flush()) or send a newline.
> >
> >
> > I assume you use Linux. Do you agree that for cross-platform language
> some
> > behavior should be made consistent?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120110/8ee3f125/attachment.html>


More information about the Python-ideas mailing list