在 Python2.x 中使用 print()(函数版本) [英] Using print() (the function version) in Python2.x
问题描述
我理解语句和表达式的区别,我理解 Python3 把 print() 变成了一个函数.
I understand the difference between a statement and an expression, and I understand that Python3 turned print() into a function.
然而,我在各种 Python2.x 解释器上运行了一个用括号括起来的 print() 语句并且它运行完美,我什至不需要导入任何模块.
However I ran a print() statement surrounded with parenthesis on various Python2.x interpreters and it ran flawlessly, I didn't even have to import any module.
我的问题:下面的代码 print("Hello SO!")
在 Python2.x 中是作为语句还是表达式计算的?
My question: Is the following code print("Hello SO!")
evaluated as a statement or an expression in Python2.x?
推荐答案
考虑以下表达式:
a = ("Hello SO!")
a = "Hello SO!"
它们是等价的.同理,用一个语句:
They're equivalent. In the same way, with a statement:
statement_keyword("foo")
statement_keyword "foo"
也是等价的.
请注意,如果您将打印功能更改为:
Notice that if you change your print function to:
print("Hello","SO!")
您会注意到 python 2 和 python 3 之间的区别.在 python 2 中,(...,...)
被解释为元组,因为 print 是一个语句,而在 python 中3、它是一个带有多个参数的函数调用.
You'll notice a difference between python 2 and python 3. With python 2, the (...,...)
is interpteted as a tuple since print is a statement whereas in python 3, it's a function call with multiple arguments.
因此,为了回答手头的问题,print
在 python 2.x 中被评估为语句,除非您 from __future__ import print_function
(在 python 2.6 中引入)
Therefore, to answer the question at hand, print
is evaluated as a statement in python 2.x unless you from __future__ import print_function
(introduced in python 2.6)
这篇关于在 Python2.x 中使用 print()(函数版本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!