Python 3 打印不带括号 [英] Python 3 print without parenthesis
问题描述
print
曾经是 Python 2 中的语句,但现在它变成了 Python 3 中需要括号的函数.
无论如何要在 Python 3 中取消这些括号?也许通过重新定义打印功能?
所以,而不是
print ("Hello stack over Flowers")
我可以输入:
打印Hello stack over Flowers"
虽然在 Python 3 中打印时需要一对括号,但 print
后不再需要空格,因为它是一个函数.所以这只是一个额外的字符.
如果您仍然发现输入一对括号不必要地耗时",您可以执行 p = print
并以这种方式保存几个字符.因为您可以将新引用绑定到函数而不是关键字,所以您只能在 Python 3 中执行此 print
快捷方式.
Python 2:
<预><代码>>>>p = 打印文件<stdin>",第 1 行p = 打印^语法错误:无效语法Python 3:
<预><代码>>>>p = 打印>>>p('你好')你好这会降低代码的可读性,但每次打印时都会保存这几个字符.
The print
used to be a statement in Python 2, but now it became a function that requires parenthesis in Python 3.
Is there anyway to suppress these parenthesis in Python 3? Maybe by re-defining the print function?
So, instead of
print ("Hello stack over flowers")
I could type:
print "Hello stack over flowers"
Although you need a pair of parentheses to print in Python 3, you no longer need a space after print
, because it's a function. So that's only a single extra character.
If you still find typing a single pair of parentheses to be "unnecessarily time-consuming," you can do p = print
and save a few characters that way. Because you can bind new references to functions but not to keywords, you can only do this print
shortcut in Python 3.
Python 2:
>>> p = print
File "<stdin>", line 1
p = print
^
SyntaxError: invalid syntax
Python 3:
>>> p = print
>>> p('hello')
hello
It'll make your code less readable, but you'll save those few characters every time you print something.
这篇关于Python 3 打印不带括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!