问题:为什么在Python 2.7中自愿使用印刷括号?

在Python 2.7中,以下两者将执行相同的操作

print("Hello, World!") # Prints "Hello, World!"

print "Hello, World!" # Prints "Hello, World!"

但是以下内容不会

print("Hello,", "World!") # Prints the tuple: ("Hello,", "World!")

print "Hello,", "World!" # Prints the words "Hello, World!"

在Python 3.x中,括号print是强制性的,本质上使其成为一个函数,但是在2.7中,两者都可以使用不同的结果。我print在Python 2.7中还应该了解什么?

In Python 2.7 both the following will do the same

print("Hello, World!") # Prints "Hello, World!"

print "Hello, World!" # Prints "Hello, World!"

However the following will not

print("Hello,", "World!") # Prints the tuple: ("Hello,", "World!")

print "Hello,", "World!" # Prints the words "Hello, World!"

In Python 3.x parenthesis on print is mandatory, essentially making it a function, but in 2.7 both will work with differing results. What else should I know about print in Python 2.7?


回答 0

在Python 2.x print中,实际上是一个特殊的语句,而不是一个函数*。

这也是为什么不能像这样使用的原因: lambda x: print x

请注意,(expr)它不会创建元组(结果为expr),但,会创建。在方寸之间这种可能的结果print (x),并print (x, y)在Python 2.7

(1)   # 1 -- no tuple Mister!
(1,)  # (1,)
(1,2) # (1, 2)
1,2   # 1 2 -- no tuple and no parenthesis :) [See below for print caveat.]

但是,由于print是Python 2.x中的特殊语法语句/语法构造,因此,如果没有括号,它将,以特殊方式处理,并且不会创建元组。对print语句的这种特殊处理使它无论是否有尾随都可以采取不同的行动,

快乐的编码。


* printPython 2中的此行为可以更改为Python 3:

from __future__ import print_function

In Python 2.x print is actually a special statement and not a function*.

This is also why it can’t be used like: lambda x: print x

Note that (expr) does not create a Tuple (it results in expr), but , does. This likely results in the confusion between print (x) and print (x, y) in Python 2.7

(1)   # 1 -- no tuple Mister!
(1,)  # (1,)
(1,2) # (1, 2)
1,2   # 1 2 -- no tuple and no parenthesis :) [See below for print caveat.]

However, since print is a special syntax statement/grammar construct in Python 2.x then, without the parenthesis, it treats the ,‘s in a special manner – and does not create a Tuple. This special treatment of the print statement enables it to act differently if there is a trailing , or not.

Happy coding.


*This print behavior in Python 2 can be changed to that of Python 3:

from __future__ import print_function

回答 1

这一切都非常简单,与向前或向后兼容性无关。

print在版本3之前的所有Python版本中,该语句的一般形式为:

print expr1, expr2, ... exprn

(依次对每个表达式求值,将其转换为字符串并在它们之间显示一个空格。)

但是请记住,在表达式周围加上括号仍然是相同的表达式。

因此,您也可以这样写:

print (expr1), (expr2), ... (expr3)

这与调用函数无关。

It’s all very simple and has nothing to do with forward or backward compatibility.

The general form for the print statement in all Python versions before version 3 is:

print expr1, expr2, ... exprn

(Each expression in turn is evaluated, converted to a string and displayed with a space between them.)

But remember that putting parentheses around an expression is still the same expression.

So you can also write this as:

print (expr1), (expr2), ... (expr3)

This has nothing to do with calling a function.


回答 2

在谈到UTF-8时,我们有一个有趣的副作用。

>> greek = dict( dog="σκύλος", cat="γάτα" )
>> print greek['dog'], greek['cat']
σκύλος γάτα
>> print (greek['dog'], greek['cat'])
('\xcf\x83\xce\xba\xcf\x8d\xce\xbb\xce\xbf\xcf\x82', '\xce\xb3\xce\xac\xcf\x84\xce\xb1')

最后打印的是带有十六进制字节值的元组。

Here we have interesting side effect when it comes to UTF-8.

>> greek = dict( dog="σκύλος", cat="γάτα" )
>> print greek['dog'], greek['cat']
σκύλος γάτα
>> print (greek['dog'], greek['cat'])
('\xcf\x83\xce\xba\xcf\x8d\xce\xbb\xce\xbf\xcf\x82', '\xce\xb3\xce\xac\xcf\x84\xce\xb1')

The last print is tuple with hexadecimal byte values.


回答 3

基本上在Python 3之前的Python中,print是一个特殊的语句,如果作为参数获取,则打印所有字符串。因此,print "foo","bar"仅表示“先打印’foo’,再打印’bar’”。这样做的问题是,很容易将print当作一个函数来使用,而Python语法对此却模棱两可,因为它(a,b)是一个包含ab只是foo(a,b)对两个参数的函数的调用的元组。

因此,他们对3进行了不兼容的更改,以减少程序的歧义性和规则性。

(实际上,我认为2.7的行为与2.6的行为相同,但我不确定。)

Basically in Python before Python 3, print was a special statement that printed all the strings if got as arguments. So print "foo","bar" simply meant “print ‘foo’ followed by ‘bar'”. The problem with that was it was tempting to act as if print were a function, and the Python grammar is ambiguous on that, since (a,b) is a tuple containing a and b but foo(a,b) is a call to a function of two arguments.

So they made the incompatible change for 3 to make programs less ambiguous and more regular.

(Actually, I think 2.7 behaves as 2.6 did on this, but I’m not certain.)


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。