[Tutor] different behaviour in Idle shell vs Mac terminal
steve at pearwood.info
Mon Jan 9 00:56:29 CET 2012
Adam Gold wrote:
> I have short piece of code I'm using to print a string to the terminal one letter at a time. It works fine when I invoke the script from within Idle; each letter appears after the preceding one according to the designated time interval. However if I run it in the Mac terminal ('python3 ./script.py'), there's a pause and then the whole string prints in one go. Here's the relevant code:
> import sys
> import time
> text = "this text is printing one letter at a time..."
> for char in text:
> I'm thinking this may be a tty issue (is stdout going to the right terminal?)
It's a buffering issue.
> P.S. if it's relevant, this is part of a simple financial maths program and it's used to display the results after certain inputs have been gathered.
To annoy your users? I'm not sure why you think it's a good idea to pretend
that the computer has to type the letters one at a time. This isn't some
stupid imaginary program in a Hollywood movie, I assume it is meant to
actually be useful and usable, and trust me on this, waiting while the program
pretends to type gets old *really* fast.
(What are you trying to emulate? A stock ticker or something? Do those things
still even exist? I haven't seen one since the last time I watched the Addams
Family TV series. The old one, in black & white.)
But if you must, after writing each character, call sys.stdout.flush() to
flush the buffer.
More information about the Tutor