Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2to3 adding extra parenthesis around functional argument

I'm a bit baffled why 2to3 is bothering embracing my print arguments that are already in functional style to be wrapped in an extra set of parenthesis. For example

print("\t[Warn] Can not connect {}".format(ssid))

becomes

print(("\t[Warn] Can not connect {}".format(ssid)))

Are these essentially conservative false positives? I'm thinking maybe the trailing ) in the format function is throwing its logic.

like image 615
jxramos Avatar asked Feb 28 '18 01:02

jxramos


People also ask

What happens when you call a function without parentheses in Python?

In another case, when we call a function without parentheses, a function reference is sent to the callable rather than executing the function itself. Let’s discuss 3 important concepts:

How do you add arguments to a function in Python?

Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma. The following example has a function with one argument (fname). When the function is called, we pass along a first name, which is used inside the function to print the full name:

What are Python optional arguments and how to master them?

When you master Python optional arguments, you’ll be able to define functions that are more powerful and more flexible. To get the most out of this tutorial, you’ll need some familiarity with defining functions with required arguments.

What happens when you code a sub-function with parentheses?

Since the merge_string is used without parentheses, the concatenate_string passes the function reference to the callable func rather than executing the merge_string. Hence, we can conclude that when we code sub-function with parentheses in a return statement, the main function executes the sub-function and passes the result to the callable.


1 Answers

From the documentation

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.

2to3 Docs

like image 175
Ermin Avdagic Avatar answered Oct 01 '22 16:10

Ermin Avdagic