问题描述
例如:
def my_function(arg_1=None, arg_2=0):
determination = arg_1 or arg_2 or 'no arguments given!'
print(determination)
return determination
当不带参数调用时,上述函数将打印并返回 'no arguments given!'
When called with no arguments, the above function would print and return 'no arguments given!'
Python 为什么要这样做,如何才能最好地利用这一功能?
Why does Python do this, and how can one best make best use of this functionality?
推荐答案
or
"表达式在赋值时的作用:
我们有时会在 Python 中看到这样的示例,以替代具有三元赋值的条件表达式,(实际上,它有助于激发语言添加条件语句).
What the "or
" expression does on assignment:
We sometimes see examples of this in Python as a substitute for conditional expression with ternary assignments, (in fact, it helped inspire the language to add conditional statements).
x = a or b
如果bool(a)
返回False
,则x
被赋值为b
这里有一个这样的条件表达式的例子,它完成了同样的事情,但可能没有那么神秘.
Here's an example of such a conditional expression that accomplishes the same thing, but with perhaps a bit less mystery.
def my_function(arg_1=None, arg_2=0):
determination = arg_1 if arg_1 else arg_2 if arg_2 else 'no arguments given!'
print(determination)
return determination
重复这种语法太多被认为是不好的风格,否则 它的对于单行来说还可以. 缺点是它 有点重复.
Repeating this syntax too much is considered to be bad style, otherwise it's OK for one-liners. The downside is that it is a bit repetitive.
基本情况,x 或 y
如果 bool(x)
评估为 True
,则返回 x
,否则返回评估 y
,(查看文档以供参考).因此,一系列 or
表达式具有返回评估 True
的第一项或最后一项的效果.
The base case, x or y
returns x
if bool(x)
evaluates True
, else it evaluates y
, (see the docs for reference). Therefore, a series of or
expressions has the effect of returning the first item that evaluates True
, or the last item.
例如
'' or [] or 'apple' or () or set(['banana'])
返回 'apple'
,第一个计算结果为 True
的项目,以及
returns 'apple'
, the first item that evaluates as True
, and
'' or [] or ()
返回 ()
,即使它的计算结果为 False
.
returns ()
, even though it evaluates as False
.
相比之下,如果 bool(x)
评估为 False
,则 x 和 y
返回 x
,否则返回返回 y
.
For contrast, x and y
returns x
if bool(x)
evaluates as False
, else it returns y
.
当您考虑到条件 and
系列中的所有条件都需要评估为 True
and 会以这种方式工作是有道理的> 让控制流沿着这条路径前进,并且当您遇到 False
的项目时继续评估这些项目是没有意义的.
It makes sense that and
would work this way when you consider that all of the conditions in a conditional and
series needs to evaluate as True
for the control flow to proceed down that path, and that it makes no sense to continue evaluating those items when you come across one that is False
.
使用 and
进行赋值的实用性并不像使用 or
那样立即明显,但它在历史上曾用于三元赋值.也就是说,在这种更清晰和直接的构造可用之前:
The utility of using and
for assignment is not immediately as apparent as using or
, but it was historically used for ternary assignment. That is, before this more clear and straightforward construction was available:
a = x if condition else y
用布尔运算符形成的等价物是:
the equivalent formed with boolean operators was:
a = condition and x or z # don't do this!
虽然它的含义是基于对 Python and
和 or
评估的充分理解而得出的,但它的可读性不如三元条件,最好完全避免.
which while the meaning is derivable based on a full understanding of Python and
and or
evaluation, is not nearly as readable as the ternary conditional, and is best avoided altogether.
必须小心使用布尔表达式进行赋值.绝对不要使用 and
进行赋值,这很容易混淆,很容易出错.样式专家会发现使用 or
的赋值不太可取(比更冗长的三元,if
条件 else
),但我发现它在专业的 Python 社区中如此普遍,以至于它可以被认为是惯用的.
Using Boolean expressions for assignment must be done carefully. Definitely never use and
for assignment, which is confusing enough to be quite error-prone. Style mavens will find use of or
for assignments less preferable (than the more verbose ternary, if
condition else
), but I have found that it is so common in the professional Python community that it could be considered idiomatic.
如果您选择使用它,请谨慎使用,并理解如果达到最终元素,无论其评估如何,都将始终返回,因此最终元素可能应该是 literal,这样你就知道你的变量有一个很好的默认后备.
If you choose to use it, do so cautiously with the understanding that the final element, if reached, will always be returned regardless of its evaluation, so that final element should probably be a literal, so that you know you have a good default fallback for your variable.
这篇关于Python 中的“x = y 或 z"赋值有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!