Output from sys.stdout in interactive mode

前端 未结 1 1876
[愿得一人]
[愿得一人] 2020-12-12 02:00

I tested sys.stdout.write in interactive mode; why do I get the \'extra\' 1 and 2 suffixed to the numbers? If I run the code from a file I get the expected output (1234...)

相关标签:
1条回答
  • 2020-12-12 02:27

    Python is also echoing the return value of sys.stdout.write() call, which is the number of bytes written:

    >>> import sys
    >>> written = sys.stdout.write('10')
    10>>> written
    2
    

    Here the next prompt follows the '10' written without a newline.

    Or, as a different way of demoing, writting 0 bytes in a loop prints 0 that many times:

    >>> for i in range(3):
    ...     sys.stdout.write('')
    ... 
    0
    0
    0
    
    0 讨论(0)
提交回复
热议问题