Message248457
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. |
|
Date |
User |
Action |
Args |
2015-08-12 12:16:45 | steven.daprano | set | recipients:
+ steven.daprano, r.david.murray, flying sheep |
2015-08-12 12:16:45 | steven.daprano | set | messageid: <1439381805.05.0.246766437692.issue24849@psf.upfronthosting.co.za> |
2015-08-12 12:16:45 | steven.daprano | link | issue24849 messages |
2015-08-12 12:16:44 | steven.daprano | create | |
|