[issue34494] simple "sequence" class ignoring __len__

Josh Rosenberg report at bugs.python.org
Sun Aug 26 10:43:43 EDT 2018


Josh Rosenberg <shadowranger+python at gmail.com> added the comment:

That's the documented behavior. Per https://docs.python.org/3/reference/datamodel.html#object.__getitem__ :

>Note: for loops expect that an IndexError will be raised for illegal indexes to allow proper detection of the end of the sequence. 

The need for *only* __getitem__ is also mentioned in the documentation of the iter builtin ( https://docs.python.org/3/library/functions.html#iter ):

>Without a second argument, object must be a collection object which supports the iteration protocol (the __iter__() method), or it must support the sequence protocol (the __getitem__() method with integer arguments starting at 0).

At no point is a dependency on __len__ mentioned.

----------
nosy: +josh.r

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34494>
_______________________________________


More information about the Python-bugs-list mailing list