R. David Murray added the comment:
Guido specifically rejected __len__ for iterators when the iteration protocol
was designed. This has become a FREQ in recent times (Frequently Rejected
Enhancement Request :)
The rationale, as I understand it, is that an iterator object should always
evalu
Steven D'Aprano added the comment:
Not every trivial one-liner needs to be in the standard library. In this case,
there are two easy (and obvious) ways to do it:
sum(1 for x in iterator)
len(list(iterator))
(The first probably saves memory; the second probably is faster.)
Given how rare it
New submission from bugale bugale :
I have noticed that there is no convenient way in python to get the number of
items in a generator.
For example:
my_iterable = iter(range(1000))
len(my_iterable) # Would not work
Of course, something like this would ruin the generator, and it will no lon