为什么'和'和;在Python“或”返回操作数? [英] Why do 'and' & 'or' return operands in Python?

查看:166
本文介绍了为什么'和'和;在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.

根据文档: http://docs.python.org/2/library /stdtypes.html

操作和内置函数具有布尔结果总是返回 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: http://learnpythonthehardway.org/book/ex28.html
为什么测试和测试回归测试或 1和1 1的回报,而不是真实的吗?
Python和许多语言像它返回一个操作数自己的布尔前pressions而不只是真或假。这意味着,如果你做了假,1你得到的第一个操作数(假),但如果你真和1将获得第二(1)。玩这一点。

According to LPTHW: http://learnpythonthehardway.org/book/ex28.html Why 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.

推荐答案

我觉得你莫名其妙地感到困惑的文档怎么说。看看这两个文档部分:<一href=\"http://docs.python.org/2/library/stdtypes.html?highlight=short%20circuit#truth-value-testing\">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真正的,除非另有说明。 (重要的例外:布尔操作或和,并始终返回他们的一个操作数)

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

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

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操作数。您可以在维基百科页面阅读更多有关短路评价。

For and operator, if the expression is true, it will return the last operand, if the expression is false, it will return the first falsey operand. You can read more about Short Circuit Evaluation at the Wikipedia Page.

您有很多你的问题的例子,让我们来分析一下其中的一些:

You have a lot of examples in your question, let's analyze some of them:

>>> False and 1  # return false (short circuited at first falsey value)
False
>>> True and 1   # return 1 (never short circuited and return the last truthy value)
1
>>> 1 and False  # return false (short circuited at first falsey value, in this case the last operand)
False
>>> 1 and True  # return True (never short circuited and return the last truthy value)
True
>>> True and 121  # return 121 (never short circuited and return the last truthy value)
121
>>> False or 1  # return 1 (since the first operand was falsey, or kept looking for a first truthy value which happened to be the last operator)
1
>>> False or 112  # return 112 for same reason as above
112
>>> False or "Khadijah"  # return "Khadijah" for same reason as above
'Khadijah'
>>> True and 'Khadijah'  # return "Khadijah" because same reason as second example
'Khadijah'

我想这应该做出点。为了帮助您进一步了解为什么这是有用的,请看下面的例子:

I think this should make a point. To help you further understand why this is useful, consider the following example:

您有一个随机生成的名称功能

You have a function that randomly generate names

import random

def generate_name():
    return random.choice(['John', 'Carl', 'Tiffany'])

和你有一个变量,你不知道,如果它已指派一个名字又如此,而不是做的:

and you have a variable that you don't know if it has assigned a name yet so instead of doing:

if var is None:
    var = generate_name()

您可以做oneliner:

You can do oneliner:

var = var or generate_name()

由于是falsey值,将继续寻求和评估第二个操作数,这就是,通话该函数返回最终生成的名字。这是一个非常愚蠢的例子,这种风格的我已经看到更好的惯例(尽管不是在Python)。我不能用一个更好的例子,现在出来了。您还可以看看这个问题,还有的话题非常有用的答案:是否Python支持短路?

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“或”返回操作数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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