This is a hack, but if you really want to have len work on a general iterable (consuming it in the way), you can create your own version of len.
The len function is essentially equivalent to the following (though implementations usually provide some optimizations to avoid the extra lookup):
def len(iterable):
return iterable.__len__()
Therefore we can define our new_len to try that, and if __len__ does not exist, count the number of elements ourselves by consuming the iterable:
def new_len(iterable):
try:
return iterable.__len__()
except AttributeError:
return sum(1 for _ in iterable)
The above works in Python 2/3, and (as far as I know) should cover every conceivable kind of iterable.