Skip to content

Commit 1f28227

Browse files
committed
pythongh-124285: document assumptions on __bool__/__len__ behaviour
1 parent 165ed68 commit 1f28227

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

‎Doc/reference/datamodel.rst‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2037,12 +2037,15 @@ Basic customization
20372037
.. index:: single: __len__() (mapping object method)
20382038

20392039
Called to implement truth value testing and the built-in operation
2040-
``bool()``; should return ``False`` or ``True``. When this method is not
2040+
:func:`bool`; should return ``False`` or ``True``. When this method is not
20412041
defined, :meth:`~object.__len__` is called, if it is defined, and the object is
20422042
considered true if its result is nonzero. If a class defines neither
20432043
:meth:`!__len__` nor :meth:`!__bool__`, all its instances are considered
20442044
true.
20452045

2046+
Two successive calls to :meth:`!__bool__` on the same object must
2047+
return same value.
2048+
20462049

20472050
.. _attribute-access:
20482051

@@ -2947,6 +2950,9 @@ through the object's keys; for sequences, it should iterate through the values.
29472950
:meth:`~object.__bool__` method and whose :meth:`!__len__` method returns zero is
29482951
considered to be false in a Boolean context.
29492952

2953+
Two successive calls to :meth:`!__len__` on the same object must
2954+
return same value.
2955+
29502956
.. impl-detail::
29512957

29522958
In CPython, the length is required to be at most :data:`sys.maxsize`.

0 commit comments

Comments
 (0)