python 2.7中的print和print()有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33996749/
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 is the difference between print and print() in python 2.7
提问by Tail of Godzilla
I am newbie on Python.
我是 Python 新手。
I run the following code on python 2.7 and I see different result when I use print or print(). What is the difference between these two functions? I read other questions e.g., this question, but I didn't find my answer.
我在 python 2.7 上运行以下代码,当我使用 print 或 print() 时看到不同的结果。这两个函数有什么区别?我阅读了其他问题,例如这个问题,但我没有找到我的答案。
class Rectangle:
def __init__(self, w, h):
self.width = w
self.height = h
def __str__(self):
return "(The width is: {0}, and the height is: {1})".format(self.width, self.height)
box = Rectangle(100, 200)
print ("box: ", box)
print "box: ", box
The result is:
结果是:
('box: ', <__main__.Rectangle instance at 0x0293BDC8>)
box: (The width is: 100, and the height is: 200)
采纳答案by RemcoGerlich
In Python 2.7 (and before), print
is a statementthat takes a number of arguments. It prints the arguments with a space in between.
在 Python 2.7(及之前)中,print
是一个带有多个参数的语句。它打印参数,中间有一个空格。
So if you do
所以如果你这样做
print "box:", box
It first prints the string "box:", then a space, then whatever box
prints as (the result of its __str__
function).
它首先打印字符串“box:”,然后是一个空格,然后box
打印为(其__str__
函数的结果)。
If you do
如果你这样做
print ("box:", box)
You have given oneargument, a tuple consisting of two elements ("box:" and the object box
).
您给出了一个参数,一个由两个元素(“box:”和 object box
)组成的元组。
Tuples print as their representation (mostly used for debugging), so it calls the __repr__
of its elements rather than their __str__
(which should give a user-friendly message).
元组打印作为它们的表示(主要用于调试),因此它调用__repr__
其元素的 ,而不是它们的__str__
(应该给出用户友好的消息)。
That's the difference you see: (The width is: 100, and the height is: 200)
is the result of your box's __str__
, but <__main__.Rectangle instance at 0x0293BDC8>
is its __repr__
.
这就是你看到的区别:(The width is: 100, and the height is: 200)
是你的盒子的结果__str__
,但<__main__.Rectangle instance at 0x0293BDC8>
它的__repr__
.
In Python 3 and higher, print()
is a normal function as any other (so print(2, 3)
prints "2 3"
and print 2, 3
is a syntax error). If you want to have that in Python 2.7, put
在 Python 3 及更高版本中,print()
与其他任何函数一样是正常函数(因此print(2, 3)
打印"2 3"
并且print 2, 3
是语法错误)。如果你想在 Python 2.7 中使用它,把
from __future__ import print_function
at the top of your source file, to make it slightly more ready for the present.
在源文件的顶部,使其更适合现在。
回答by chuwy
In the first example you're printing tuple, but not calling print function. So the following is identical:
在第一个示例中,您正在打印元组,但不调用打印函数。所以下面是相同的:
a = ("box: ", box)
print a
In other words, in first example you've created a tuple and printed it. Different output, for different datatypes.
换句话说,在第一个示例中,您创建了一个元组并打印了它。不同的输出,针对不同的数据类型。
There's supposed to be no significant difference between function and statement for your case, but for sake of future I'm strongly advice you to always use function (print()
). However, if you're still interested in differences (not related to your case), with print function you can specify separator, end and where to output it as described in documentation.
对于您的情况,function 和 statement 之间应该没有显着差异,但是为了将来,我强烈建议您始终使用 function ( print()
)。但是,如果您仍然对差异感兴趣(与您的情况无关),则可以使用打印功能指定分隔符、结束符以及输出位置,如文档中所述。
回答by Serge Ballesta
This in mainly a complement to other answers.
这主要是对其他答案的补充。
You can see in Python 2 scripts print (var)
when the normal usage would be print var
.
您可以在 Python 2 脚本中看到print (var)
正常使用时print var
.
It uses the fact that (var)
is just a parenthesed expression in Python 2 with is simply seen as var
so print(var)
and print var
behaves exactly the same in Python 2 - but only works when printing one single variable
它使用了一个事实,(var)
即在 Python 2 中只是一个带括号的表达式,在 Python 2 中被简单地视为var
如此print(var)
并且print var
行为完全相同 - 但仅在打印单个变量时有效
The interesting point is that when you considere a migration to Python 3, print(var)
(here the call to functionprint) is already the correct syntax.
有趣的一点是,当您考虑迁移到 Python 3 时,print(var)
(此处调用函数print)已经是正确的语法。
TL/DR: print(var)
in Python 2 is just a trick to ease Python3 migration using the fact that (var)
is just an expression - the tuple form would be (var,)
TL/DR:print(var)
在 Python 2 中只是使用(var)
只是一个表达式的事实来简化 Python3 迁移的一个技巧- 元组形式将是(var,)
回答by Trop Freshlo?c
In python 2.7 print() is faster than print. Here a test make with Repl.it Python Console Online:
在 python 2.7 中,print() 比打印快。这里使用Repl.it Python Console Online进行测试:
import time
start_time = time.time()
print "lol"
end_time = time.time()
print(end_time - start_time)
start_time_2 = time.time()
print ("lol")
end_time_2 = time.time()
print(end_time_2 - start_time_2)
print((end_time_2 - start_time_2) > (end_time - start_time))
Python 2.7.10
[GCC 4.8.2] on linux
lol
7.08103179932e-05
lol
1.00135803223e-05
False
回答by Chanpreet Chhabra
In Python 2.7: print is not a function, it is a keyword and acts as a special statement.
在 Python 2.7 中:print 不是函数,而是关键字并充当特殊语句。
Before that, we need to learn more about tuple and expr in python2.
在此之前,我们需要了解更多关于python2中的tuple和expr。
If we write ('hello'): it is regarded as an expression, not a tuple, it can be only be regarded as a tuple if "," is introduced.
如果我们写('hello'):它被视为一个表达式,而不是一个元组,只有引入“,”才能被视为一个元组。
eg:
例如:
print("hello")
>>> hello
a=("hello")
>>>'hello'
a=("hello", "world")
>>>('hello', "world")
print("hello", "world")
>>> ('hello', 'world')
so in python2.7, when we use print statement with parenthesis, it is still regarded as a statement not a python function and values with parenthesis are treated to be a tuple or an expression while in python 3, print is no longer a statement it is considered to be a function.
所以在python2.7中,当我们使用带括号的print语句时,它仍然被视为语句而不是python函数,带括号的值被视为元组或表达式,而在python 3中,print不再是一个语句被认为是一个函数。
In order to use the print function of python 3 in python2 use the following import statement,
为了在python2中使用python 3的打印功能,请使用以下import语句,
from __future__ import print_function