蟒蛇"或QUOT;运营商怪异的行为 [英] python "or" operator weird behavior

查看:120
本文介绍了蟒蛇"或QUOT;运营商怪异的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,code:

>>> False or 'hello'
'hello'

这令人惊讶的行为让你检查如果x =无,在一个行来检查的x值:

This surprising behavior lets you check if x != None and check x value in one line:

>>> x = 10 if randint(0,2)==1 else None
>>> (x or 0) > 0
depend on x value...

解释:或像这样的功能:(链接
如果x是假的,那么y,否则x

Explanation: "or" functions like this: (link) "if x is false, then y, else x"

没有,我知道的语言可以让你做到这一点。
那么,为什么Python的?

No language that i know lets you do this. So, why does Python?

推荐答案

这听起来像是你将两个问题合而为一。

It sounds like you're combining two issues into one.

首先,有短路的问题。马辛的回答解决这个问题很好,所以我不会尝试做任何好转。

First, there's the issue of short-circuiting. Marcin's answer addresses this issue perfectly, so I won't try to do any better.

二,有返回的最后一个评估值,而不是将其转换为bool。还有要进行两种方式的参数,你可以找到在鸿沟的两边许多语言。

Second, there's or and and returning the last-evaluated value, rather than converting it to bool. There are arguments to be made both ways, and you can find many languages on either side of the divide.

返回的最后一个评估值允许 functionCall(x)或设置defaultValue 快捷方式,避免了可能浪费的转换(为什么转换的 INT 2 布尔 1 如果你会用它做的唯一事情就是检查它是否是非零?),并且通常更容易解释。因此,对于这些原因的各种组合,如C,Lisp语言,使用Javascript,Lua中,Perl和Ruby和VB的所有语言做事这种方式,也是如此Python的。

Returning the last-evaluated value allows the functionCall(x) or defaultValue shortcut, avoids a possibly wasteful conversion (why convert an int 2 into a bool 1 if the only thing you're going to do with it is check whether it's non-zero?), and is generally easier to explain. So, for various combinations of these reasons, languages like C, Lisp, Javascript, Lua, Perl, Ruby, and VB all do things this way, and so does Python.

始终从操作返回一个布尔值,有助于捕捉一些错误(尤其是在逻辑运算符和位运算符是容易混淆的语言),它可以让你的设计,其中布尔检查严格类型的检查的语言为真正,而不是只检查非零,它使运营商的类型更容易写出来,而且避免了处理转换为那里的两个操作数是不同的情况下,类型(请参阅运营商C-家族语言)。因此,对于这些原因的各种组合,如C ++,Fortran语言,Smalltalk和Haskell的所有语言做事这样的。

Always returning a boolean value from an operator helps to catch some errors (especially in languages where the logical operators and the bitwise operators are easy to confuse), and it allows you to design a language where boolean checks are strictly-typed checks for true instead of just checks for nonzero, it makes the type of the operator easier to write out, and it avoids having to deal with conversion for cases where the two operands are different types (see the ?: operator in C-family languages). So, for various combinations of these reasons, languages like C++, Fortran, Smalltalk, and Haskell all do things this way.

在你的问题(如果我理解正确的话),你使用该功能,能够写出这样的:

In your question (if I understand it correctly), you're using this feature to be able to write something like:

if (x or 0) < 1:

X 可以很容易地。这种特殊的用例是不是很有用,一方面是因为更明确的 X如果x否则为0 (在Python 2.5和更高版本)也很容易编写,可能更容易要了解(至少圭多是这样认为的),而且还因为无&LT; 1 相同 0℃; 1 反正(至少在Python 2.x中,所以你总是有两个选项中的至少一个)......但也有类似的例子在那里的的有用。比较这两种:

When x could easily be None. This particular use case isn't very useful, both because the more-explicit x if x else 0 (in Python 2.5 and later) is just as easy to write and probably easier to understand (at least Guido thinks so), but also because None < 1 is the same as 0 < 1 anyway (at least in Python 2.x, so you've always got at least one of the two options)… But there are similar examples where it is useful. Compare these two:

return launchMissiles() or -1

return launchMissiles() if launchMissiles() else -1

第二个将浪费大量的导弹炸​​毁了在南极的敌人两次而非一次。

The second one will waste a lot of missiles blowing up your enemies in Antarctica twice instead of once.

如果你好奇为什么Python做这种方式:

If you're curious why Python does it this way:

早在1.x的日子里,已的任何布尔类型。你有falsy值如 0 [] ()等,以及其他一切是真实的,那么谁需要明确的?返回 1 将是愚蠢的,因为 1 没有比更真实[1,2,3] dsfsdf。到时候加入布尔(逐渐超过两x版本,IIRC),目前的逻辑已经牢固地嵌入语言,改变将会破坏了很多code。

Back in the 1.x days, there was no bool type. You've got falsy values like None, 0, [], (), "", etc., and everything else is true, so who needs explicit False and True? Returning 1 from or would have been silly, because 1 is no more true than [1, 2, 3] or "dsfsdf". By the time bool was added (gradually over two 2.x versions, IIRC), the current logic was already solidly embedded in the language, and changing would have broken a lot of code.

那么,为什么他们不改变它在3.0吗?许多Python用户,包括BDFL圭多,会建议(因为这是违反TOOWTDI的最起码),你不应该在这种情况下,使用;您应该存储在一个变量的前pression,例如的结果是:

So, why didn't they change it in 3.0? Many Python users, including BDFL Guido, would suggest that you shouldn't use or in this case (at the very least because it's a violation of "TOOWTDI"); you should instead store the result of the expression in a variable, e.g.:

missiles = launchMissiles()
return missiles if missiles else -1

而事实上,圭多曾表示,他希望禁止 launchMissiles()或-1 ,这也是他最终接受了三元的原因<$一部分C $ C>如果 - 其他前pression说他以前拒绝了很多次。但很多人不同意,和Guido是的仁者的DFL。此外,使工作,你期待其他地方的方式,同时拒绝做你想做的(但圭多不希望你想要的)这里,实际上是pretty复杂。

And in fact, Guido has stated that he'd like to ban launchMissiles() or -1, and that's part of the reason he eventually accepted the ternary if-else expression that he'd rejected many times before. But many others disagree, and Guido is a benevolent DFL. Also, making or work the way you'd expect everywhere else, while refusing to do what you want (but Guido doesn't want you to want) here, would actually be pretty complicated.

所以,Python将可能永远是代替同一侧的Java,Smalltalk和Haskell中在同一侧为C,Perl和Lisp的这里,

So, Python will probably always be on the same side as C, Perl, and Lisp here, instead of the same side as Java, Smalltalk, and Haskell.

这篇关于蟒蛇&QUOT;或QUOT;运营商怪异的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆