On Sun, 15 Jul 2012 16:33:23 +0200 Christian Heimes <li...@cheimes.de> wrote: > Am 15.07.2012 16:22, schrieb Antoine Pitrou: > > On Mon, 16 Jul 2012 00:08:41 +1000 > > Nick Coghlan <ncogh...@gmail.com> wrote: > >> Right, I agree on the value in being able to return something to say "this > >> cannot be converted to a concrete container". > > > > Who would be able to return that, apart from trivial cases like > > itertools.cycle()? > > For example most numerical sequence iterators like Fibonacci generator, > prime number sequence generator and even trivial cases like even natural > number generator.
First, you can't implement __length_hint__ for a generator, which is the preferred (the most practical) way of writing iterators in pure Python. Second, not all iterators will implement __length_hint__ (because it's optional and, really, of rather little use). So, as a user, you cannot hope that `list(some_iterator)` will always raise instead of filling your memory with an infinite stream of values: you have to be careful anyway. Even if __length_hint__ is implemented, its result may be wrong. That's the whole point: it's a *hint*; an iterator might tell you it's finite while it's infinite, or the reverse. My conclusion is that an infinite iterator is a documentation issue. Just tell the user that it doesn't stop, and let them shoot themselves in the foot in they want to. Regards Antoine. -- Software development and contracting: http://pro.pitrou.net _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com