17xie > Python 语言参考手册 > 5.10 布尔运算 Boolean operations
背景:                 
[本书目录] [图书首页] [本书讨论区]  
链接地址:http://www.17xie.com/read-37446.html    注册17xie 一起来写书 实现您的出书梦想!


5.10 布尔运算 Boolean operations

Boolean operations have the lowest priority of all Python operations:

布尔运算符在所有Python运算符中有最低的优先级:

Download entire grammar as text.

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, 而不是'')



5.11 Lambda形式(lambda表达式) Lambdas

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形式创建的函数不能包括语句.


字数:2611    最后更新:8个月以前 [03-15 20:57]月落晨星 修改
本页编辑者:月落晨星  
[前一页]:5.8 二元位运算符 Bin…  [后一页]:5.12 表达式表 Expres…
[在本页中加入书签] [收藏本书] [推荐本书]
  17xie论坛 > 本书讨论区 > 本页评论   (共0条)
发表评论

用户名称 匿名发表
评论内容
验证码

关于我们 | 版权声明 | 免责声明 | 诚聘英才 | 联系我们 | 合作伙伴 | 友情链接 | 广告合作 | 提交意见
Copyright © 2007 17xie.com 互联网协同写书平台 京ICP备08002671号