From 1f2822735f3dfe7b67215b31d0a146888b7be5ce Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sat, 28 Sep 2024 08:55:10 +0300 Subject: [PATCH] gh-124285: document assumptions on __bool__/__len__ behaviour --- Doc/reference/datamodel.rst | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 5ce6bf17db41eab..0ce1930ece0ccea 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -2037,12 +2037,15 @@ Basic customization .. index:: single: __len__() (mapping object method) Called to implement truth value testing and the built-in operation - ``bool()``; should return ``False`` or ``True``. When this method is not + :func:`bool`; should return ``False`` or ``True``. When this method is not defined, :meth:`~object.__len__` is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither :meth:`!__len__` nor :meth:`!__bool__`, all its instances are considered true. + Two successive calls to :meth:`!__bool__` on the same object must + return same value. + .. _attribute-access: @@ -2947,6 +2950,9 @@ through the object's keys; for sequences, it should iterate through the values. :meth:`~object.__bool__` method and whose :meth:`!__len__` method returns zero is considered to be false in a Boolean context. + Two successive calls to :meth:`!__len__` on the same object must + return same value. + .. impl-detail:: In CPython, the length is required to be at most :data:`sys.maxsize`.