【问题标题】:What is this print syntax? (print rightshift)这个打印语法是什么? (打印右移)
【发布时间】:2011-11-07 10:42:12
【问题描述】:

看着the source code of pstats我看到了这个语法:

print >> self.stream, "in %.3f seconds" % self.total_tt
print >> self.stream

这个语法是什么,它是如何调用的以及如何使用它?我以前从未见过它,也没有在我读过的任何 Python 书籍/教程中看到它。

【问题讨论】:

    标签: python syntax printing python-2.7 bit-shift


    【解决方案1】:

    如果您的意思是>>,那就是Python 2.x 的语法,用于使用print 写入sys.stdout 以外的类似文件。它在 Python 文档 6.6. The print statement 中定义,并且至少从 Python 2.5 开始就已经存在(我认为更早)。

    在 Python 3.0 中,此语法已被 file kwarg 替换为 print 函数。

    【讨论】:

    • 将其视为 python 版本的 fprintf 也可能会有所帮助,但您指定的不是 c 的文件句柄,而是 python“类文件对象”。
    【解决方案2】:

    它是 print 语句的扩展形式,它将输出重定向到紧随其后的类文件对象。请参阅 Python docs

    【讨论】:

      猜你喜欢
      • 2013-07-23
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多