Iterator length

Gabriel Genellina gagsl-py at yahoo.com.ar
Thu Jan 18 23:25:35 EST 2007


At Thursday 18/1/2007 20:26, bearophileHUGS at lycos.com wrote:

>def leniter(iterator):
>     """leniter(iterator): return the length of an iterator,
>     consuming it."""
>     if hasattr(iterator, "__len__"):
>         return len(iterator)
>     nelements = 0
>     for _ in iterator:
>         nelements += 1
>     return nelements
>
>Is it a good idea to extend the functionalities of the built-in len
>function to cover such situation too?

I don't think so, because it may consume the iterator, and that's a 
big side effect that one would not expect from builtin len()


-- 
Gabriel Genellina
Softlab SRL 


	

	
		
__________________________________________________ 
Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 
http://www.yahoo.com.ar/respuestas 




More information about the Python-list mailing list