本文介绍了为什么'和'和;在Python“或”返回操作数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要通过LPTHW和我遇到一些我无法理解来了。当将它永远是你希望你的布尔的情况下返回比布尔其他的东西吗?该LPTHW文指出,像蟒蛇所有语言都有这种行为。他将意味着PTED与类型VS静态类型语言的编译语言或鸭间$ P $?

I'm going through the LPTHW and I came across something I cannot understand. When will it ever be the case that you want your boolean and or or to return something other than the boolean? The LPTHW text states that all languages like python have this behavior. Would he mean interpreted vs. compiled languages or duck typed vs static typed languages?

我跑到下面的code:

I ran the following code:

>>> False and 1
False
>>> True and 1
1
>>> 1 and False
False
>>> 1 and True
True
>>> True and 121
121
>>> False or 1
1
>>> False or 112
112
>>> False or "Khadijah"
'Khadijah'
>>> True and 'Khadijah'
'Khadijah'
>>> False or 'b'
'b'
>>> b = (1, 2, "K")
>>> b
(1, 2, 'K')
>>> False or b
(1, 2, 'K')
>>>

请帮助我了解什么怎么回事。

Please help me understand whats going on here.

根据文档:

操作和内置函数具有布尔结果总是返回 0 为假, 1 真正的,除非另有说明。 (重要的例外:布尔运算总是回到自己的一个操作数)

Operations and built-in functions that have a Boolean result always return 0 or False for false and 1 or True for true, unless otherwise stated. (Important exception: the Boolean operations or and and always return one of their operands.)

据LPTHW:
为什么测试和测试回归测试或 1和1 1的回报,而不是真实的吗?
Python和许多语言像它返回一个操作数自己的布尔前pressions而不只是真或假。这意味着,如果你做了假,1你得到的第一个操作数(假),但如果你真和1将获得第二(1)。玩这一点。

According to LPTHW: http://learnpythonthehardway.org/book/ex28.htmlWhy does "test" and "test" return "test" or 1 and 1 return 1 instead of True?Python and many languages like it return one of the operands to their boolean expressions rather than just True or False. This means that if you did False and 1 you get the first operand (False) but if you do True and 1 your get the second (1). Play with this a bit.

推荐答案

我觉得你莫名其妙地感到困惑的文档怎么说。看看这两个文档部分:Truth值测试和布尔运算符。引用的拳头节最后一段:

I think you're somehow confused about what the docs says. Take a look at these two docs sections: Truth Value Testing and Boolean Operators. To quote the last paragraph on the fist section:

操作和内置函数具有布尔结果总是假,1或True返回0或False真正的,除非另有说明。 (重要的例外:布尔操作或和,并始终返回他们的一个操作数)

正如你所看到的,你是正确的操作和内置函数的,但看到的重要的例外的一部分,它是很好说,布尔运算符的将返回其操作数。

As you can see, you're right about operations and built-in functions but see the Important exception part, it is well stated that the Boolean operators will return one of their operands.

现在,他们可以返回很难对运营商的短路逻辑依赖。对于运营商,它将返回第一的 truthy 的在EX pression价值,因为当它找到一个,整个前pression是真实的。在每一个操作数为的情况下的 falsey 将返回最后一个操作数,这意味着它遍历他们每个人不能够找到一个truthy之一。

Now, what they can return depends hardly on the operator's short circuit logic. For or operator, it will return the first truthy value in the expression, since when it finds one, the whole expression is true. In case of every operand being falsey, or will return the last operand, meaning that it iterated over every one of them not being able to find a truthy one.

有关运营商,如果EX pression是真实的,它会返回最后一个操作数,如果EX pression是假的,它会返回第一falsey操作数。您可以在维基百科页面阅读更多有关

Since None is a falsey value, or will continue its search and evaluate second operand, this is, call the function ultimately returning the generated name. This is a very silly example, I have seen better usages (although not in Python) of this kind of style. I couldn't come out with a better example right now. You can also take a look at this questions, there are very useful answers on the topic: Does Python support short-circuiting?

最后但并非最不重要的,这无关静态类型化,类型化的鸭,动态,除preTED,编译任何语言。这只是一个语言特性,一个可能出现得心应手,这是很常见的,因为几乎所有的编程语言,我能想到提供此功能。

Last but not least, this has nothing to do with static typed, duck typed, dynamic, interpreted, compiled, whatever language. It's just a language feature, one that might come handy and that is very common since almost every programming language I can think of provide this feature.

希望这有助于!

这篇关于为什么'和'和;在Python“或”返回操作数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 23:30