Boolean operations have the lowest priority of all Python operations:
布尔运算符在所有Python运算符中有最低的优先级:
expression |
::= |
or_test |
lambda_form |
or_test |
::= |
and_test |
or_test "or"
and_test |
and_test |
::= |
not_test |
and_test "and"
not_test |
not_test |
::= |
comparison | "not"
not_test |
lambda_form |
::= | "lambda" [parameter_list]:
expression |
In the context of Boolean operations, and also when expressions
are used by control flow statements, the following values are
interpreted as false: None, numeric zero of all types,
empty sequences (strings, tuples and lists), and empty mappings
(dictionaries). All other values are interpreted as true.
在布尔运算的上下文, 和控制流语句使用的表达式中, 以下值解释为假:None, 所有类型的数值零,空的有序类型对象(串, 元组和列表),空的映射对象(字典). 所有其它值解释为真.
The operator not yields 1
if its argument is false, 0 otherwise.
如果运算符not的参数为假, 它返回1, 否则返回0.
The expression x and y first
evaluates x; if x is false, its value is
returned; otherwise, y is evaluated and the resulting
value is returned.
表达式x and y首先计算x; 如果x为假, 就返回它的值; 否则, 计算y的值, 并返回其结果.
The expression x or y first
evaluates x; if x is true, its value is
returned; otherwise, y is evaluated and the resulting
value is returned.
表达式x or y首先计算x; 如果x为真, 就返回它的值; 否则, 计算y的值, 并返回其结果.
(Note that neither and nor or restrict the value and type they return to
0 and 1, but rather return the last
evaluated argument. This is sometimes useful, e.g., if
s is a string that should be replaced by a default
value if it is empty, the expression s or 'foo' yields
the desired value. Because not has to
invent a value anyway, it does not bother to return a value of the
same type as its argument, so e.g., not 'foo' yields
0, not ''.)
(注意and和or都没有限制它的结果的值和类型必须是0或1, 仅仅是最后一个计算的参数. 这在某些情况下是有用的,例如, 如果s是一个若为空就应该为默认值所替换的串, 表达式s or 'foo'就会得到希望的结果. 因为not根本不会生成一个值, 就没必要让它的返回值类型与其参数的相同, 所以, 例如, not 'foo' 返回0, 而不是'')
Lambda forms (lambda expressions) have the same syntactic
position as expressions. They are a shorthand to create anonymous
functions; the expression lambda arguments:
expression yields a function object. The unnamed
object behaves like a function object defined with
Lambda形式(lambda表达式)在句法上与表达式有相同的位置. 这是一个创建类型函数的快捷方法;表达式lambda arguments: expression生成一个行为与下面定义的函数一致的函数对象:
def name(arguments):
return expression
See section 7.5 for the syntax of parameter lists. Note that functions created with lambda forms cannot contain statements.
对于参数表句法,参见7.5节.注意由lambda形式创建的函数不能包括语句.