在Python布尔前pressions评价 [英] Evaluation of boolean expressions in Python

查看:107
本文介绍了在Python布尔前pressions评价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

做对象在Python评估什么真值?

相关问题


  • 在Python 对象的布尔值:浅谈覆盖方式,评估


解决方案

  

任何对象都可以对真理进行测试
  值,使用IF或同时
  条件或作为布尔操作
  下面的操作。以下值
  被认为是假的:


  
  

      

  •   

  •   
  • 任何数值类型,例如, 0 0L 0.0 , 0J


  •   
  • 任何空序列,例如() []


  •   
  • 任何空的映射,例如, {}


  •   
  • 用户定义的类的实例,如果类定义了一个 __非零__() __ LEN __()方法,当该方法返回整数零或bool值


  •   

  
  

所有其他值都认为是真
   - 这样许多类型的对象是总是如此。
  操作和内置函数具有布尔结果总是返回0或假,1或为真, 除非另有说明。 (重要的例外:布尔运算或与和总是返回自己的一个操作数)


<一个href=\"https://docs.python.org/2/library/stdtypes.html#truth-value-testing\">https://docs.python.org/2/library/stdtypes.html#truth-value-testing

和如前所述,你可以通过修改非零与自定义对象覆盖。

What truth value do objects evaluate to in Python?

Related Questions

解决方案

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, 0L, 0.0, 0j.

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

  • any empty mapping, for example, {}.

  • instances of user-defined classes, if the class defines a __nonzero__() or __len__() method, when that method returns the integer zero or bool value False.

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

https://docs.python.org/2/library/stdtypes.html#truth-value-testing

And as mentioned, you can override with custom objects by modifying nonzero.

这篇关于在Python布尔前pressions评价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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