gpt4 book ai didi

python - Python 中 boolean 表达式的求值

转载 作者:太空狗 更新时间:2023-10-29 18:15:28 24 4
gpt4 key购买 nike

Python 中对象的真值是多少?

相关问题

最佳答案

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

如前所述,您可以通过修改非零值来覆盖自定义对象。

关于python - Python 中 boolean 表达式的求值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1452489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com