gpt4 book ai didi

python - Python 中的双大于号

转载 作者:太空狗 更新时间:2023-10-29 21:31:22 30 4
gpt4 key购买 nike

我正在查看一些 Python 源代码,当我遇到这个时:

print >> sys.stderr, __doc__

>>是什么意思?我以前从未见过这样的语法。

最佳答案

请参阅 Python 2.7 docs 中的“print chevron”说明:

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.

特殊语法在 Python 3 中消失了,因为 print 从语句转换为函数。

关于python - Python 中的双大于号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29223647/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com