开发者

stdout and stderr anomalies

from the inte开发者_StackOverflow中文版ractive prompt:

>>> import sys
>>> sys.stdout.write('is the')
is the6

what is '6' doing there?

another example:

>>> for i in range(3):
...     sys.stderr.write('new black')
... 
9
9
9
new blacknew blacknew black

where are the numbers coming from?


In 3.x the write method of a file object returns the number of bytes written, and the interactive prompt prints out the return value of whatever you are running. So you print out 'is the' (6 bytes), and the interpreter then prints out 6 (the return from write). See the relevant docs for 3.1.

This does not happen before 3.0 as the write method returned None, and therefore nothing was printed.


They are the return values from write, printed by the interactive shell.

Try

>>> 3

what happens?


This happens on Python3, but not Python2.

Mike is correct that the write in Python3 is returning the number of bytes written, which is then being printed by the interactive shell.

the write in Python2 returned None, so nothing was printed

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜