This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Correct Should to Must in Definition of object.__len__
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Michal Kononenko, brett.cannon, docs@python
Priority: normal Keywords:

Created on 2019-04-25 14:52 by Michal Kononenko, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg340844 - (view) Author: Michal Kononenko (Michal Kononenko) Date: 2019-04-25 14:52
The link below defines __len__

https://docs.python.org/3/reference/datamodel.html?highlight=__len__#object.__len__

However, I was reading in the StackOverflow thread below that CPython does some validation to check that the return value of __len__ should be >= 0. Does this mean that len must return a value >= 0, in the RFC 2119 sense of the word?

https://stackoverflow.com/questions/42521449/how-does-python-ensure-the-return-value-of-len-is-an-integer-when-len-is-cal
msg340851 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2019-04-25 17:53
There is probably validation code in len(), but the method itself can do whatever it wants if you call it directly. So the documentation is accurate in saying the method _should_ return >= 0 but that there's nothing strictly enforcing that in Python for that specific method when defined and called directly.
History
Date User Action Args
2022-04-11 14:59:14adminsetgithub: 80901
2019-04-25 17:53:14brett.cannonsetstatus: open -> closed

nosy: + brett.cannon
messages: + msg340851

resolution: not a bug
stage: resolved
2019-04-25 14:52:18Michal Kononenkocreate