[Python-Dev] PEP 0424: A method for exposing a length hint (original) (raw)
Antoine Pitrou solipsis at pitrou.net
Sun Jul 15 17:06:28 CEST 2012
- Previous message: [Python-Dev] PEP 0424: A method for exposing a length hint
- Next message: [Python-Dev] PEP 0424: A method for exposing a length hint
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Sun, 15 Jul 2012 16:33:23 +0200 Christian Heimes <lists at cheimes.de> wrote:
Am 15.07.2012 16:22, schrieb Antoine Pitrou: > On Mon, 16 Jul 2012 00:08:41 +1000 > Nick Coghlan <ncoghlan at 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
- Previous message: [Python-Dev] PEP 0424: A method for exposing a length hint
- Next message: [Python-Dev] PEP 0424: A method for exposing a length hint
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]