Suppose we have in Python 3.x (and I guess in Python 2.6 and in Python 2.7 too) the following functions:
>>> def dbl_a(p): return p*2
>>> d
In (almost) any case, when in doubt, use parenthesis!
Seeing as they all do exactly the same, I think it's a matter of personal preference. I would choose the last one.
But then again, I'm not familiar with python.
There are generally 4 uses for the parentheses ()
in Python.
\
. This is mostly stylistic.
decision = (is_female and under_30 and single
or
is_male and above_35 and single)
the parenthesis is an alternative syntax to avoid hitting the 80 column limit and having to use \
for statement continuation.
In any other cases, such as inside the if
, while
, for
predicates and the return
statement I'd strongly recommend not using ()
unless necessary or aid readability (defined by the 4 points above). One way to get this point across is that in math, (1)
and just 1
means exactly the same thing. The same holds true in Python.
People coming from the C-family of languages will take a little bit getting used to this because the ()
are required in control-flow predicates in those languages for historical reasons.
Last word for return
statements, if you are only returning 1 value, omit the ()
. But if you are returning multiple values, it's OK to use ()
because now you are returning a grouping, and the ()
enforces that visually. This last point is however stylistic and subject to preference. Remember that the return
keywords returns the result of a statement. So if you only use ,
in your multiple assignment statements and tuple constructions, omit the ()
, but if you use ()
for value unpacking and tuple constructions, use ()
when you are returning multiple values in return
. Keep it consistent.
return_stmt ::= “return” [expression_list]
expression_list
return may only occur syntactically nested in a function definition, not within a nested class definition.
If an expression list is present, it is evaluated, else None is substituted.
return leaves the current function call with the expression list (or None) as return value.
When return passes control out of a try statement with a finally clause, that finally clause is executed before really leaving the function.
In a generator function, the return statement indicates that the generator is done and will cause StopIteration to be raised. The returned value (if any) is used as an argument to construct StopIteration and becomes the StopIteration.value attribute.
In an asynchronous generator function, an empty return statement indicates that the asynchronous generator is done and will cause StopAsyncIteration to be raised. A non-empty return statement is a syntax error in an asynchronous generator function.
So basically return is a statement with an optional expression list as an argument. Therefore parentheses are not required and only preferrable when necesary (i.e. for breaking precedences).
Generally the convention would be to not use brackets on a return statement, as it's not a function. In this case, the parser would simply be ignoring the brackets entirely.
Tuples are typically expressed with brackets to increase readability, however they are not required.
Ultimately, however, they are all functionality identical and it comes down to preference.
return value
is the "correct" way - return is a language construct, not a function.
If you want to return a tuple, use return your, values, here
There's no need for any parenthesis (tuples are created by the ,
"operator", not the ()
)