Should a return statement have parentheses?

前端 未结 5 1689
独厮守ぢ
独厮守ぢ 2020-12-08 01:57

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         


        
相关标签:
5条回答
  • 2020-12-08 02:27

    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.

    0 讨论(0)
  • 2020-12-08 02:32

    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.

    0 讨论(0)
  • 2020-12-08 02:37

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

    0 讨论(0)
  • 2020-12-08 02:40

    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.

    0 讨论(0)
  • 2020-12-08 02:44

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

    0 讨论(0)
提交回复
热议问题