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.

Author steven.daprano
Recipients flying sheep, r.david.murray, steven.daprano
Date 2015-08-12.12:16:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1439381805.05.0.246766437692.issue24849@psf.upfronthosting.co.za>
In-reply-to
Content
Unfortunately, this fails because there is no way to tell how long an arbitrary iterable is, or whether it is reentrant or not. Consider:

def gen():
    while True:
        if random.random() < 0.5:
            return random.random()

Not only is it not reentrant, but you cannot tell in advance how long it will be.

There's also the problem that not all iterables need to have a defined length. The iterator protocol, for example, does not demand that iterators define a length, and we should not put that burden on the programmer.

There's one more serious problem with the idea of giving iterators a length. Consider this case:

it = iter([1, 2, 3, 4, 5])
next(it)
next(it)
print(len(it))

What should be printed? 5, the length of the underlying list, or 3, the number of items still remaining to be seen? Whichever answer you give, it will be misleading and a bug magnet under certain circumstances.

I don't believe it is worth giving iterators like map, zip etc. a length depending on the nature of what they are iterating over. That can only lead to confusion. Programmers just have to understand that sequences have lengths, but arbitrary iterables may not.
History
Date User Action Args
2015-08-12 12:16:45steven.dapranosetrecipients: + steven.daprano, r.david.murray, flying sheep
2015-08-12 12:16:45steven.dapranosetmessageid: <1439381805.05.0.246766437692.issue24849@psf.upfronthosting.co.za>
2015-08-12 12:16:45steven.dapranolinkissue24849 messages
2015-08-12 12:16:44steven.dapranocreate