[issue32065] range_iterator doesn't have length, leads to surprised result

yegle report at bugs.python.org
Fri Nov 17 18:11:12 EST 2017


New submission from yegle <cnyegle at gmail.com>:

This also affects xrange in Python2, so I chose the affected version as python27.

range object (and xrange in Python2) has __len__(), but the range_iterator object created from __reversed__() doesn't have __len__.

Python2:
>>> x = xrange(10)
>>> len(x)
10
>>> reversed(x)
<rangeiterator object at 0x7f86065684e0>
>>> y = reversed(x)
>>> len(y)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'rangeiterator' has no len()

Python3.6
>>> x = range(10)
>>> len(x)
10
>>> len(reversed(x))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'range_iterator' has no len()

----------
components: Interpreter Core
messages: 306458
nosy: yegle
priority: normal
severity: normal
status: open
title: range_iterator doesn't have length, leads to surprised result
versions: Python 2.7

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


More information about the Python-bugs-list mailing list