On Fri, Jun 12, 2020 at 09:01:33AM +0100, Rob Cliffe via Python-Dev wrote:
If I run the following program (using Python 3.8.3 on a Windows 10 laptop):
import sys, time for i in range(1,11): sys.stdout.write('\r%d' % i)
In Python 2, the 'write()` method returns None, which is suppressed in the REPR. In Python 3, the `write` method returns the number of bytes (or characters, I forget which...) actually written, which is not suppressed. I've been bitten by this myself, forgetting that in a script any result not bound to a variable gets silently thrown away, but in the REPL it gets printed.
It appears that the requested characters are output, *followed by* the number of characters output (which is the value returned by sys.stdout.write) and a newline. Surely this is not the intended behaviour.
Of course it is. The whole point of the REPL is to evaluate an expression and have the result printed. (That's the P in REPL :-) `stdout.write(...)` is an expression that returns a value, so the REPL prints it. -- Steven