From 09486310b6a74c68ffad8f32ab15889755acd30a Mon Sep 17 00:00:00 2001 From: Peter Thomassen Date: Fri, 30 Jun 2017 12:53:24 +0200 Subject: [PATCH 1/3] bpo-30803: clarify truth value testing documentation --- Doc/library/stdtypes.rst | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index e0691c7dcf87d46..356439fd6f47d3c 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 +* numeric zero of any 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 From 8c7a89cd04c41dc414ece99dd56801e702a6dcad Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Sat, 29 Jul 2017 14:59:06 -0400 Subject: [PATCH 2/3] News blurb. --- .../next/Documentation/2017-07-29-14-55-50.bpo-30803.6hutqQ.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Documentation/2017-07-29-14-55-50.bpo-30803.6hutqQ.rst diff --git a/Misc/NEWS.d/next/Documentation/2017-07-29-14-55-50.bpo-30803.6hutqQ.rst b/Misc/NEWS.d/next/Documentation/2017-07-29-14-55-50.bpo-30803.6hutqQ.rst new file mode 100644 index 000000000000000..4699713ee6e286e --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2017-07-29-14-55-50.bpo-30803.6hutqQ.rst @@ -0,0 +1 @@ +Clarify doc on truth value testing. Original patch by Peter Thomassen. From 649d1908f4b6115507f4cf922a0c50fc505d4e8b Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Sat, 29 Jul 2017 15:05:45 -0400 Subject: [PATCH 3/3] Update stdtypes.rst --- Doc/library/stdtypes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 356439fd6f47d3c..7f6a6ac3ad86542 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -54,7 +54,7 @@ objects considered false: * constants defined to be false: ``None`` and ``False``. -* numeric zero of any type: ``0``, ``0.0``, ``0j``, ``Decimal(0)``, +* zero of any numeric type: ``0``, ``0.0``, ``0j``, ``Decimal(0)``, ``Fraction(0, 1)`` * empty sequences and collections: ``''``, ``()``, ``[]``, ``{}``, ``set()``,