Steven D'Aprano added the comment:

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.

----------
nosy: +steven.daprano

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue24849>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to