pylint says “Unnecessary parens after %r keyword”

Each Answer to this Q is separated by one/two green lines.

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

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(…)

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

print('Hello')

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

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

Which explained here.

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

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

from __future__ import print_function

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

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.

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

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

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.


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .