Python built-in types - Truth Value Testing

Python内建类型之真值检测(Truth Value Testing)

Manual

Any object can be tested for truth value, for use in an if  or while condition or as operand of the Boolean operations below. The following values are considered false:

  • None

  • False

  • zero of any numeric type, for example,0, 0.0, 0j.

  • any empty sequence, for example, '',(), [].

  • any empty mapping, for example, {}.

  • instances of user-defined classes, if the class defines a __bool__() or __len__() method, when that method returns the integer zero orbool valueFalse

All other values are considered true — so objects of many types are always true.

Operations and built-in functions that have a Boolean result always return0 orFalse for false and1 orTrue for true, unless otherwise stated. (Important exception: the Boolean operationsor andand always return one of their operands.)

直译

任意对象可以通过‘使用 if 或 while条件’或‘在布尔预算中作为操作数’来检测真值。以下值被认为是

  • None
  • False
  • 任意数值类型的0值,例如:0, 0.0, 0j
  • 任意空序列,例如:‘’,[],()
  • 任意空映射,例如:{}
  • 自定义类的实例,如果类定义了__bool__()或__len__()方法,当方法返回整数0或布尔值False
除此之外,所有其他值被认为是,所以通常很多类型的对象为。运算和内建函数的布尔运算结果,往往为否返回0或False,为真返回1或True(除非有其他规定)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值