@@ -39,31 +39,26 @@ Truth Value Testing
39
39
single: false
40
40
41
41
Any object can be tested for truth value, for use in an :keyword: `if ` or
42
- :keyword: `while ` condition or as operand of the Boolean operations below. The
43
- following values are considered false:
42
+ :keyword: `while ` condition or as operand of the Boolean operations below.
44
43
45
- .. index :: single: None (Built-in object)
46
-
47
- * ``None ``
48
-
49
- .. index :: single: False (Built-in object)
50
-
51
- * ``False ``
52
-
53
- * zero of any numeric type, for example, ``0 ``, ``0.0 ``, ``0j ``.
44
+ .. index :: single: true
54
45
55
- * any empty sequence, for example, ``'' ``, ``() ``, ``[] ``.
46
+ By default, an object is considered true unless its class defines either a
47
+ :meth: `__bool__ ` method that returns ``False `` or a :meth: `__len__ ` method that
48
+ returns zero, when called with the object. [1 ]_ Here are most of the built-in
49
+ objects considered false:
56
50
57
- * any empty mapping, for example, ``{} ``.
51
+ .. index ::
52
+ single: None (Built-in object)
53
+ single: False (Built-in object)
58
54
59
- * instances of user-defined classes, if the class defines a :meth: `__bool__ ` or
60
- :meth: `__len__ ` method, when that method returns the integer zero or
61
- :class: `bool ` value ``False ``. [1 ]_
55
+ * constants defined to be false: ``None `` and ``False ``.
62
56
63
- .. index :: single: true
57
+ * zero of any numeric type: ``0 ``, ``0.0 ``, ``0j ``, ``Decimal(0) ``,
58
+ ``Fraction(0, 1) ``
64
59
65
- All other values are considered true --- so objects of many types are always
66
- true.
60
+ * empty sequences and collections: `` '' ``, `` () ``, `` [] ``, `` {} ``, `` set() ``,
61
+ `` range(0) ``
67
62
68
63
.. index ::
69
64
operator: or
0 commit comments