无聊之人--除了技术,还是技术,你懂得
分类: Python/Ruby
2011-06-16 15:30:22
. The Peculiar Nature of and and or
And &or 的奇特性质
In Python, and and or perform boolean logic as you would expect, but they do not return boolean values; instead, they return one of the actual values they are comparing.
在Python中,正如你所期望的那样,and&or 完成布尔测试,但是它们并不返回布尔值,相反它们只返回它们所比较值得实际值。
Example 4.15. Introducing and
例4.15 and 简介
When using and, values are evaluated in a boolean context from left to right. 0, '', [], (), {}, and None are false in a boolean context; everything else is true. Well, almost everything. By default, instances of classes are true in a boolean context, but you can define special methods in your class to make an instance evaluate to false. You'll learn all about classes and special methods in Chapter 5. If all values are true in a boolean context, and returns the last value. In this case, and evaluates 'a', which is true, then 'b', which is true, and returns 'b'. 当使用and的时候,在布尔上下文的环境中对表达式自左至右进行求值,其中0,’’,[],(),[]以及None都为假;其他的所有对象都为真值。是的,几乎所有对象都为真值。通常,在布尔测试中类实例为真,但是在你自己的类中你可以定义一个特定的方法来然你的类实例在布尔测试时为假。在第五章将会学习到类和特定的方法。在布尔测试中,如果所有值都为真,那么返回最后一个值。在本例中,首先对’a’求值,为真,接着对b求值,也是真,从而返回b. |
|
If any value is false in a boolean context, and returns the first false value. In this case, '' is the first false value. 在布尔测试中任何一个值为假,那么将返回第一个求值为假的值。在本例中,’’是第一个求值为假的值。 |
|
All values are true, so and returns the last value, 'c'. 所有的值求值都为真,因此返回最后一二真值’c’。 |
Example 4.16. Introducing or
例4.16 or 简介
When using or, values are
evaluated in a boolean context from left to right, just like and. If any
value is true, or returns that value immediately. In this case, 'a' is
the first true value. 在使用or的过程中,同and 一样,布尔测试也是从左至右进行求值。如果存在一个真值,or 则立即返回改值。在本例中,’a’是第一个真值。 or evaluates '', which is
false, then 'b', which is true, and returns 'b'. or 对’’求值,为假,接着对’b’b 求值,为真,从而返回’b’. If all values are false, or returns
the last value. or evaluates '', which is false, then [],
which is false, then {}, which is false, and returns {}. 如果所有的值都为假,or 返回最后一个值。Or 对’’求值,为假,接着对[]求值,还是假,最后对{}求值仍为假,从而返回最后一个值 {}。 Note that or evaluates values
only until it finds one that is true in a boolean context, and then it
ignores the rest. This distinction is important if some values can have side
effects. Here, the function sidefx is never called, because or evaluates 'a',
which is true, and returns 'a' immediately. 注意 or在进行布尔测试求值时,只有遇到测试为真时才停止求值,并忽略剩余的值。这种特性是很重要的,如果某些值会引发副作用。这儿,函数sidefx从不会调用,因为Or对’a’进行求值,结果为真,直接返回’a’.
If you're a C hacker, you are certainly familiar with the bool ? a : b expression, which evaluates to a if bool is true, and b otherwise. Because of the way and and or work inPython, you can accomplish the same thing.
如果你是一个C高手,你一定非常熟悉 bool? a:b 表达式,该表达式对bool 求值,如果为真,则返回a,否则返回b .因为在Python and 和Or的奇特方式,你也能做同样的事。
4.6.1. Using the and-or Trick
And-or 使用技巧
Example 4.17. Introducing the and-or Trick
例4.17 and –or 技巧简介
This syntax looks similar to the bool ? a : b expression in C. The entire expression is evaluated from left to right, so the and is evaluated first. 1 and 'first' evalutes to'first', then 'first' or 'second' evalutes to 'first'. 该表达式看起来和C中的冒号表达是类似。争个表达式自左至右求值,因此首先对and 求值,1 和first 求值 的结果是first,然后 first 和 seond 进行or 运算,结果为first. |
|
0 and 'first' evalutes to False, and then 0 or 'second' evaluates to 'second'. O 和 first 求值后假,接着 0和 second 求值后为真,返回second. |
However, since this Python expression is simply boolean logic, and not a special construct of the language, there is one extremely important difference between this and-ortrick in Python and the bool ? a : b syntax in C. If the value of a is false, the expression will not work as you would expect it to. (Can you tell I was bitten by this? More than once?)
然而,因为上面的Python表达式是简单的布尔逻辑运算并不是语言的特殊构成部分,C中bool? A: b 和Python中的and –or 技巧的区别还是很大的。如果a的值为假,那么表达式的值可能会和你期望的不一样。你能说我痴迷于此么?经常?)
Example 4.18. When the and-or Trick Fails
例4.18 and_or 技巧失效时
Since a is an empty string,
which Python considers false in a boolean context, 1 and '' evalutes
to '', and then '' or 'second' evalutes to 'second'.
Oops! That's not what you wanted. 因为a 是一个空字符串在布尔测试中,Python视作假值,1 he ‘‘进行and运算后为’‘,接着’’同second
进行or 运算,接结果为’second’.哦,这不是你想要的。
The and-or trick, bool and a or b, will not work like the C expression bool ? a : b when a is false in a boolean context.
在布尔测试中,当对a 求值为假时,and –or 技巧 bool and a or b 的效果不同于C语言中的冒号表达式。
The real trick behind the and-or trick, then, is to make sure that the value of a is never false. One common way of doing this is to turn a into [a] and b into [b], then taking the first element of the returned list, which will be either a or b.
那么and-or 的技巧的真正用途是确保a 的值绝对不能为假。解决这个问题的一个通用方法就是将a包装[a],b包装成[b],那么返回列表中第一个运算的值要么是a ,要么是b。
Example 4.19. Using the and-or Trick Safely
例4.19 安全使用and-or 技巧
Since [a] is a non-empty
list, it is never false. Even if a is 0 or '' or
some other false value, the list [a] is true because it has one
element. 因为[a]是一个非空列表,它的布尔求值后结果为真。即使 a是0,或是’’,或是其它假值,但是li[a]为真,应该该列表含有一个元素。
By now, this trick may seem like more trouble than it's worth. You could, after all, accomplish the same thing with an if statement, so why go through all this fuss? Well, in many cases, you are choosing between two constant values, so you can use the simpler syntax and not worry, because you know that the a value will always be true. And even if you need to use the more complicated safe form, there are good reasons to do so. For example, there are some cases in Python where if statements are not allowed, such as in lambda functions.
现在,这个技巧产生的问题远远多于它所带来的好处。毕竟,你可以使用if语句来完场相同的事情,那为什么要做这种小题大做的事情呢?恩,在大多数的情况下,在两个常量值之间进行选择的时候,你会使用简单的语法而且不用担心,因为你知道a 永远都是真值。即使你打算使用略为复杂安全的形式,使用这种技巧也是有理由的。比如Python在某些情况必须使用这种技巧,因为if语句不允许使用的,比如lamd函数。