Python pylint 说“%r 关键字后有不必要的括号”

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

pylint says "Unnecessary parens after %r keyword"

pythonpython-2.7python-3.x

提问by setevoy

After my first CodeReview Q- I got tip in answer:

在我的第一个 CodeReview Q 之后- 我得到了回答的提示:

Your code appears to be for Python 2.x. To be a bit more ready for a possible future migration to Python 3.x, I recommend to start writing your print ... statements as print(...)

您的代码似乎适用于 Python 2.x。为了为将来可能迁移到 Python 3.x 做好更多准备,我建议您开始将 print ... 语句编写为 print(...)

Thus, in my following code (I'm using Python 2.6 and 2.7 on my boxes) I always us ()for print:

因此,在我的以下代码中(我在我的盒子上使用 Python 2.6 和 2.7)我总是我们()print

print('Hello')

Today I first time test my code with PyLint, and it says:

今天我第一次用 PyLint 测试我的代码,它说:

C: 43, 0: Unnecessary parens after 'print' keyword (superfluous-parens)

C: 43, 0: 'print' 关键字后不必要的括号(多余的括号)

Which explained here.

这里解释

So - does print(str)is really incorrect, or I can disregard this PyLint messages?

那么 -print(str)真的不正确,还是我可以忽略这个 PyLint 消息?

采纳答案by SmCaterpillar

To make pylint aware that you want to use the new print statement and not put erroneous brackets simply use

要让 pylint 知道您想使用新的打印语句而不是放置错误的括号,只需使用

from __future__ import print_function

at the beginning of your script. This has also the advantage that you alwaysneed to use print(...)instead of print .... Accordingly, your program will throw a SyntaxErrorin case you fall back to the old syntax.

在脚本的开头。这也有一个优点,你总是需要使用print(...)而不是print .... 因此,SyntaxError如果您退回到旧语法,您的程序将抛出 a 。

Be aware that this does not work in python 2.5 or older. But since you use 2.6 and 2.7, there should be no problem.

请注意,这在 python 2.5 或更早版本中不起作用。不过既然你用的是2.6和2.7,应该没有问题。

回答by danielfranca

In Python 3 print is a function, which requires the (). In Python 2 it's not, so the parents are unnecessary.

在 Python 3 中,print 是一个函数,它需要 ()。在 Python 2 中不是,所以父母是不必要的。

If you will migrate your code to Python 3 in the future it's good to keep the habit to put ().

如果您将来要将代码迁移到 Python 3,最好保持 put () 的习惯。

https://docs.python.org/3.0/whatsnew/3.0.html#print-is-a-functionhttps://www.python.org/dev/peps/pep-3105/

https://docs.python.org/3.0/whatsnew/3.0.html#print-is-a-function https://www.python.org/dev/peps/pep-3105/

You are probably using a Python2 pylint, that's why it throws this warning, nothing to be worried.

您可能正在使用 Python2 pylint,这就是它抛出此警告的原因,无需担心。