Evaluation of boolean expressions in Python

后端 未结 2 1710
滥情空心
滥情空心 2020-11-27 20:56

What truth value do objects evaluate to in Python?

Related Questions

  • Boolean Value of Objects in Python: Discussion about overriding t
2条回答
  •  没有蜡笔的小新
    2020-11-27 21:56

    Update: Removed all duplicate infomation with Meder's post

    For custom objects in Python < 3.0 __nonzero__ to change how it is evaluated. In Python 3.0 this is __bool__ (Reference by e-satis)

    It is important to understand what is meant by evaluate. One meaning is when an object is explicitly casting to a bool or implicitly cast by its location (in a if or while loop).

    Another is == evalutation. 1==True, 0==False, nothing else is equal via ==.

    >>> None==False
    False
    >>> 1==True
    True
    >>> 0==False
    True
    >>> 2==False
    False
    >>> 2==True
    False
    

    Finally, for is, only True or False are themselves.

提交回复
热议问题