diff options
author | Terry Jan Reedy <tjreedy@udel.edu> | 2017-07-29 22:56:06 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-29 22:56:06 (GMT) |
commit | 4c7b368de7bcabdd821059c023c46c9d85668d3f (patch) | |
tree | 74b3cacbde2caed5ba39efeeacc616f36a538b05 /Doc | |
parent | b26cc82b2252dd54e73661a026b32e7808fef945 (diff) | |
download | cpython-4c7b368de7bcabdd821059c023c46c9d85668d3f.zip cpython-4c7b368de7bcabdd821059c023c46c9d85668d3f.tar.gz cpython-4c7b368de7bcabdd821059c023c46c9d85668d3f.tar.bz2 |
[3.6] bpo-30803: clarify truth value testing documentation (GH-2508) (#2946)
Initial patch by Peter Thomassen.
(cherry picked from commit caa1280)
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/stdtypes.rst | 33 |
1 files changed, 14 insertions, 19 deletions
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index e929f0b..b8c4d59 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -39,31 +39,26 @@ Truth Value Testing single: false Any object can be tested for truth value, for use in an :keyword:`if` or -:keyword:`while` condition or as operand of the Boolean operations below. The -following values are considered false: +:keyword:`while` condition or as operand of the Boolean operations below. - .. index:: single: None (Built-in object) - -* ``None`` - - .. index:: single: False (Built-in object) - -* ``False`` - -* zero of any numeric type, for example, ``0``, ``0.0``, ``0j``. +.. index:: single: true -* any empty sequence, for example, ``''``, ``()``, ``[]``. +By default, an object is considered true unless its class defines either a +:meth:`__bool__` method that returns ``False`` or a :meth:`__len__` method that +returns zero, when called with the object. [1]_ Here are most of the built-in +objects considered false: -* any empty mapping, for example, ``{}``. + .. index:: + single: None (Built-in object) + single: False (Built-in object) -* instances of user-defined classes, if the class defines a :meth:`__bool__` or - :meth:`__len__` method, when that method returns the integer zero or - :class:`bool` value ``False``. [1]_ +* constants defined to be false: ``None`` and ``False``. -.. index:: single: true +* zero of any numeric type: ``0``, ``0.0``, ``0j``, ``Decimal(0)``, + ``Fraction(0, 1)`` -All other values are considered true --- so objects of many types are always -true. +* empty sequences and collections: ``''``, ``()``, ``[]``, ``{}``, ``set()``, + ``range(0)`` .. index:: operator: or |