Skip to content

Commit caa1280

Browse files
peterthomassenterryjreedy
authored andcommitted
bpo-30803: clarify truth value testing documentation (#2508)
Initial patch by Peter Thomassen.
1 parent ceb93f4 commit caa1280

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

Doc/library/stdtypes.rst

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,26 @@ Truth Value Testing
3939
single: false
4040

4141
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.
4443

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
5445

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:
5650

57-
* any empty mapping, for example, ``{}``.
51+
.. index::
52+
single: None (Built-in object)
53+
single: False (Built-in object)
5854

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``.
6256

63-
.. index:: single: true
57+
* zero of any numeric type: ``0``, ``0.0``, ``0j``, ``Decimal(0)``,
58+
``Fraction(0, 1)``
6459

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)``
6762

6863
.. index::
6964
operator: or
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clarify doc on truth value testing. Original patch by Peter Thomassen.

0 commit comments

Comments
 (0)