Should a return statement have parentheses?

Python

Python Problem Overview


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
>>> def dbl_b(p): return(p*2)
>>> def dbl_c(p): return (p*2)

If we run them we get:

>>> dbl_a(42)
84
>>> dbl_b(42)
84
>>> dbl_c(42)
84

The three functions provide the same result (value and type) and they seem to be equivalent.

But which of them has the more correct return statement?

Is there any side-effect in any of those definitions?

The same questions apply to the following situation with multiple values returned:

>>> def dbl_triple_a(p): return p*2, p*3
>>> def dbl_triple_b(p): return(p*2, p*3)
>>> def dbl_triple_c(p): return (p*2, p*3)

>>> dbl_triple_a(42)
(84, 126)
>>> dbl_triple_b(42)
(84, 126)
>>> dbl_triple_c(42)
(84, 126)

In this case every function returns a tuple, but my questions still remain the same.

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

There are generally 4 uses for the parentheses () in Python.

  1. It acts the same way as most of the other mainstream languages - it's a construct to force an evaluation precedence, like in a math formula. Which also means it's only used when it is necessary, like when you need to make sure additions and subtractions happen first before multiplications and divisions.
  2. It is a construct to group immutable values together in the same spirit as a similar set notation in math. We call this a tuple in Python. Tuple is also a basic type. It is a construct to make an empty tuple and force operator precedence elevation.
  3. It is used to group imported names together in import statements so you don't have to use the multi-line delimiter \. This is mostly stylistic.
  4. In long statements like


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.

Solution 3 - Python

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.

Solution 4 - Python

7.6. The return statement

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).

Solution 5 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChaos ManorView Question on Stackoverflow
Solution 1 - PythonThiefMasterView Answer on Stackoverflow
Solution 2 - PythonY.H WongView Answer on Stackoverflow
Solution 3 - PythonJames DaviesView Answer on Stackoverflow
Solution 4 - PythonRicardo ZorioView Answer on Stackoverflow
Solution 5 - PythonWill YoungView Answer on Stackoverflow