While I was trying some stuffs in my Python 3 interpreter (Python 3.4.2, installed via brew), I encountered some weird outputs I didn't expected:
>>> import sys
>>> sys.stdout.write("foo")
foo3
>>> sys.stderr.write("bar")
3
bar
After experimenting with different strings, I think the number is the length of the inputs I pass.
I've tried this in Python 2.7.8 (gives no numbers in the outputs) and Python 3.4.2 created by virtualenv
(gives the same result)
Is the output supposed to be like this?
You're right that it's the length. In Python 2, the File.write()
method returned None
. When the move was made to Python 3, the return value changed to be the number of characters written.
The reason you're getting the different output from stdout
and stderr
will probably have to do with the order in which things show up on the file handles.
For stdout
, write
outputs "foo"
then the REPL loop (in the grand tradition of PIN numbers and ATM machines) outputs the count to the same stream.
For stderr
, it's likely that the REPL loop outputs standard output first (the length) then the content of standard error.
Or, it may be totally non-deterministic, it's probably not something you should rely on, especially as the REPL loop doesn't really exist except in the interactive mode of the interpreter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With