鱼C论坛

 找回密码
 立即注册
查看: 1841|回复: 1

标准输出内容

[复制链接]
发表于 2021-10-18 23:09:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
在IDLE Shell输入如下代码<
import sys
for&#160;i&#160;in&#160;range(3):
    sys.stderr.write('hello')>
运行结果如下:
hello5
hello5
hello5,
接着输入<for i in range(3):
                                            sys.stderr.write('hello,1')>
运行结果是:
hello,l7
hello,l7
hello,l7
我没搞明白的是:为何在每个输出后面都带个统计数据?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-19 04:39:44 | 显示全部楼层
因为 write() 方法最后会返回实际上写入了多少个字符:

  1. >>> help(sys.stderr)
  2. Help on StdOutputFile in module idlelib.run object:

  3. class StdOutputFile(StdioFile)
  4. |  StdOutputFile(shell, tags, encoding='utf-8', errors='strict')
  5. |  
  6. |  Method resolution order:
  7. |      StdOutputFile
  8. |      StdioFile
  9. |      io.TextIOBase
  10. |      _io._TextIOBase
  11. |      io.IOBase
  12. |      _io._IOBase
  13. |      builtins.object
  14. |  
  15. |  Methods defined here:
  16. |  
  17. |  writable(self)
  18. |      Return whether object was opened for writing.
  19. |      
  20. |      If False, write() will raise OSError.
  21. |  
  22. |  write(self, s)
  23. |      Write string to stream.
  24. |      Returns the number of characters written (which is always equal to
  25. |      the length of the string).
  26. |  
  27. |  ----------------------------------------------------------------------
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 16:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表