Should a return statement have parentheses?

python

Solution

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

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

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

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

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

Problem

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.

Original source