How to fix print((double parentheses)) after 2to3 conversion? How to fix print((double parentheses)) after 2to3 conversion? python-3.x python-3.x

How to fix print((double parentheses)) after 2to3 conversion?


You were already printing tuples. If you were not, then you are not now either.

To illustrate, your code would have to have been using print as if it was a function:

# Python 2print(somestring)

which becomes, after translation

# Python 3print((somestring))

That's not a tuple, that's just a pair of parentheses. It results in the same output on either version. In fact, 2to3 is smart enough to drop the inner parentheses again; the actual output written is simply

# Python 3print(somestring)

However, if you were using multiple arguments in Python 2:

# Python 2print(arg1, arg2)

then you'd already be printing a tuple, because that's really:

value_to_print = (arg1, arg2)print value_to_print

So it would only be correct to preserve that behaviour in Python 3. If you see the 2to3 tool use print((....)), then it determined that you were already printing tuples.

Demo:

$ cat testprint.pyprint('not a tuple')print('two-value', 'tuple')$ python2.7 testprint.pynot a tuple('two-value', 'tuple')$ 2to3 -w -f print testprint.pyRefactoringTool: Refactored testprint.py--- testprint.py    (original)+++ testprint.py    (refactored)@@ -1,2 +1,2 @@ print('not a tuple')-print('two-value', 'tuple')+print(('two-value', 'tuple'))RefactoringTool: Files that were modified:RefactoringTool: testprint.py$ python3.7 testprint.pynot a tuple('two-value', 'tuple')

Note that this is different from using from __future__ import print_function in your Python 2 code, to disable the print statement and so making the code call the built-in print() function. The 2to3 tool already detects this case and will pass through print(...) function calls unchanged:

$ cat futureprint.pyfrom __future__ import print_functionprint('not', 'a tuple')$ python2.7 futureprint.pynot a tuple$ 2to3 -w -f print futureprint.pyRefactoringTool: No files need to be modified.$ python3.7 futureprint.pynot a tuple

You can force 2to3 to assume all your files use from __future__ import print_function, regardless, with the -p / --print-function command-line switch:

-p, --print-function  Modify the grammar so that print() is a function

However, any deliberate print (tuple_element1, tuple_element2, ...) print statements would then be mis-translated as function calls:

$ cat printtuple.pyprint ('two-value', 'tuple')$ python2.7 printtuple.py('two-value', 'tuple')$ 2to3 -w -f print -p printtuple.pyRefactoringTool: No files need to be modified.$ python3.7 printtuple.pytwo-value tuple


If your code already has print() functions you can use the -x print argument to 2to3 to skip the conversion.


Try using the -p flag. See the last note here.

When the -p is passed, 2to3 treats print as a function instead of a statement. This is useful when from __future__ import print_function is being used. If this option is not given, the print fixer will surround print calls in an extra set of parentheses because it cannot differentiate between the print statement with parentheses (such as print ("a" + "b" + "c")) and a true function call.