Suppose we have in Python 3.x (and I guess in Python 2.6 and in Python 2.7 too) the following functions:

假设我们在Python 3.x(我猜想在Python 2.6和Python 2.7中)也有以下函数:

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

在这种情况下,每个函数都返回一个元组,但我的问题仍然保持不变。

5 个解决方案

#1


20

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

Python中的括号()通常有4种用法。

  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. 它的行为与大多数其他主流语言的行为方式相同 - 它是一种强制评估优先级的结构,就像在数学公式中一样。这也意味着它只在必要时使用,比如需要确保在乘法和除法之前首先发生加法和减法。

  3. 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.
  4. 它是一种将不可变值组合在一起的构造,其精神与数学中类似的集合符号相同。我们称之为Python中的元组。元组也是一种基本类型。它是一个构造来创建一个空元组和强制运算符优先级提升。

  5. 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.
  6. 它用于在导入语句中将导入的名称组合在一起,因此您不必使用多行分隔符\。这主要是风格。

  7. In long statements like
  8. 在长期的陈述中


    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.

括号是一种替代语法,以避免达到80列限制并且必须使用\ for语句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.

在任何其他情况下,例如if,while内部,对于谓词和return语句,我强烈建议不要使用()除非必要或辅助可读性(由上述4点定义)。获得这一点的一种方法是在数学中,(1)和1只意味着完全相同的东西。在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.

来自C语言系列的人将需要一点点习惯,因为出于历史原因,这些语言中的控制流谓词需要()。

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语句的最后一个单词,如果只返回1个值,则省略()。但是如果你要返回多个值,那么使用()是可以的,因为现在你正在返回一个分组,并且()可以直观地强制执行。然而,最后一点是风格的并且受到偏好。请记住,return关键字返回语句的结果。因此,如果您只在多个赋值语句和元组结构中使用,则省略(),但如果使用()进行值解包和元组结构,则在返回多个值时使用()。保持一致。

更多相关文章

  1. Python:内联if语句别无效
  2. 【Python】 编码,en/decode函数以及print语句的一些探索
  3. linux下开启mysql慢查询,分析查询语句
  4. 我用的mysqlcc,我想看别人执行过哪些语句怎么看?
  5. sql查询每个学生的最高成绩mysql语句
  6. 一条SQL语句实现添加不重复记录
  7. SQL查找条件语句构造类
  8. 如何利用SQL语句查询数据库中所有表的名称?
  9. sql语句延时执行或者是指定时间执行

随机推荐

  1. Python解放双手系列——用python自动追踪
  2. 动画:二叉树有几种存储方式?(上)
  3. Matplotlib:先搞明白plt. /ax./ fig再画
  4. 常用统计检验的Python实现
  5. 上班摸鱼系列|Python开发命令行斗地主
  6. 使用Python进行数据降维|线性降维
  7. 一些思考和阶段小结
  8. 类和函数傻傻分不清楚?三个例子讲明白
  9. 数据分析师还是算法工程师|用数据多角度解
  10. 使用Python进行统计建模