Python 3 打印不带括号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32122868/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:03:48  来源:igfitidea点击:

Python 3 print without parenthesis

pythonprinting

提问by Laura

The printused to be a statement in Python 2, but now it became a function that requires parenthesis in Python 3.

print曾经是在Python 2中的语句,如今却成了一个需要括号在Python 3的功能。

Is there anyway to suppress these parenthesis in Python 3? Maybe by re-defining the print function?

无论如何在Python 3中抑制这些括号?也许通过重新定义打印功能?

So, instead of

所以,而不是

print ("Hello stack over flowers")

I could type:

我可以输入:

print "Hello stack over flowers"

回答by holdenweb

No. That will always be a syntax error in Python 3. Consider using 2to3to translate your code to Python 3

不。这将始终是 Python 3 中的语法错误。考虑使用2to3将您的代码转换为 Python 3

回答by holdenweb

You can't, because the only way you could do it without parentheses is having it be a keyword, like in Python 2. You can't manually define a keyword, so no.

你不能,因为没有括号的唯一方法就是让它成为一个关键字,就像在 Python 2 中一样。你不能手动定义一个关键字,所以不能。

回答by SPKB24

In Python 3, print is a function, whereas it used to be a statement in previous versions. As @holdenweb suggested, use 2to3to translate your code.

在 Python 3 中,print 是一个函数,而在以前的版本中它曾经是一个语句。正如@holdenweb 建议的那样,使用2to3来翻译您的代码。

回答by TigerhawkT3

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.

尽管在 Python 3 中需要一对括号来打印,但在 之后不再需要空格print,因为它是一个函数。所以这只是一个额外的字符。

If you still find typing a single pair of parentheses to be "unnecessarily time-consuming," you can do p = printand save a few characters that way. Because you can bind new references to functions but not to keywords, you can only do this printshortcut in Python 3.

如果您仍然发现输入一对括号“不必要地耗时”,您可以这样做p = print并以这种方式保存几个字符。因为您可以将新引用绑定到函数而不是关键字,所以您只能print在 Python 3 中执行此快捷方式。

Python 2:

蟒蛇2:

>>> p = print
  File "<stdin>", line 1
    p = print
            ^
SyntaxError: invalid syntax

Python 3:

蟒蛇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.

它会使您的代码可读性降低,但是每次打印某些内容时您都会保存这几个字符。

回答by Harry Binswanger

Use Autohotkey to make a macro. AHK is free and dead simple to install. www.autohotkey.com

使用 Autohotkey 制作宏。AHK 是免费的,而且安装起来非常简单。www.autohotkey.com

You could assign the macro to, say, alt-p:

您可以将宏分配给,例如,alt-p:

!p::send print(){Left}

That will make alt-p put out print() and move your cursor to inside the parens.

这将使 alt-p 输出 print() 并将光标移动到括号内。

Or, even better, to directlysolve your problem, you define an autoreplace and limit its scope to when the open file has the .py extension:

或者,更好的是,为了直接解决您的问题,您可以定义一个自动替换并将其范围限制为打开的文件具有 .py 扩展名时:

#IfWinActive .py            ;;; scope limiter
:b*:print ::print(){Left}   ;;; I forget what b* does. The rest should be clear 
#IfWinActive                ;;; remove the scope limitation

This is a guaranteed, painless, transparent solution.

这是一个有保证的、无痛的、透明的解决方案。

回答by TokyoMike

The AHK script is a great idea. Just for those interested I needed to change it a little bit to work for me:

AHK 脚本是个好主意。对于那些感兴趣的人,我需要对其进行一些更改才能为我工作:

SetTitleMatchMode,2         ;;; allows for a partial search 
#IfWinActive, .py           ;;; scope limiter to only python files
:b*:print ::print(){Left}   ;;; I forget what b* does
#IfWinActive                ;;; remove the scope limitation

回答by alchemy

I finally figured out the regex to change these all in old Python2 example scripts. Otherwise use 2to3.py.

我终于找到了在旧的 Python2 示例脚本中更改所有这些的正则表达式。否则使用 2to3.py。

Try it out on Regexr.com, doesn't work in NP++(?):

在 Regexr.com 上尝试一下,在 NP++(?) 中不起作用:

find:     (?<=print)( ')(.*)(')
replace: ('')

for variables:

对于变量:

(?<=print)( )(.*)(\n)
('')\n

for label and variable:

对于标签和变量:

(?<=print)( ')(.*)(',)(.*)(\n)
('',)\n

回答by michau

Using printwithout parentheses in Python 3 code is not a good idea. Nor is creating aliases, etc. If that's a deal breaker, use Python 2.

print在 Python 3 代码中不使用括号不是一个好主意。创建别名等也不是。如果这是一个交易破坏者,请使用 Python 2。

However, printwithout parentheses might be useful in the interactive shell. It's not really a matter of reducing the number of characters, but rather avoiding the need to press Shift twice every time you want to print something while you're debugging. IPythonlets you call functions without using parentheses if you start the line with a slash:

但是,print不带括号在交互式 shell 中可能很有用。这实际上不是减少字符数的问题,而是避免在调试时每次要打印某些内容时都需要按 Shift 两次。如果以斜线开头,IPython允许您在不使用括号的情况下调用函数:

Python 3.6.6 (default, Jun 28 2018, 05:43:53)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: var = 'Hello world'

In [2]: /print var
Hello world

And if you turn on autocall, you won't even need to type the slash:

如果您打开autocall,您甚至不需要键入斜杠:

In [3]: %autocall
Automatic calling is: Smart

In [4]: print var
------> print(var)
Hello world