python 这段代码是什么意思:“打印>> sys.stderr”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1987626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does this code mean: "print >> sys.stderr"
提问by zjm1126
print >> sys.stderr, "Error in atexit._run_exitfuncs:"
Why print '>>' in front of sys.stderr
?
为什么要在前面打印“>>” sys.stderr
?
Thanks.
谢谢。
回答by iamamac
This syntax means writes to a file object (sys.stderr
in this case) instead of standard output. [Link]
此语法意味着写入文件对象(sys.stderr
在本例中)而不是标准输出。[关联]
In Python 3.0, print
becomes a function instead of a statement: [Link]
在 Python 3.0 中,print
变成函数而不是语句:[链接]
print("Error in atexit._run_exitfuncs:", file=sys.stderr)
回答by James Thompson
From the Python documentation:
print also has an extended form, defined by the second portion of the syntax described above. This form is sometimes referred to as “print chevron.” In this form, the first expression after the >> must evaluate to a “file-like” object, specifically an object that has a write() method as described above. With this extended form, the subsequent expressions are printed to this file object. If the first expression evaluates to None, then sys.stdout is used as the file for output.
print 也有一个扩展形式,由上述语法的第二部分定义。这种形式有时被称为“印刷 V 形”。在这种形式中,>> 之后的第一个表达式必须评估为“类文件”对象,特别是具有上述 write() 方法的对象。使用此扩展形式,后续表达式将打印到此文件对象。如果第一个表达式的计算结果为 None,则 sys.stdout 用作输出文件。