[Python-iterators] While we're at it...

Michael Chermside mcherm at destiny.com
Fri Jun 29 09:10:00 EDT 2001


Magnus Lie Hetland wrote:
> 
> I'm sure there are plenty of similar cases (functions
> working on sequences that can be made to work on
> iterators), but how about random.choice?
> 

I don't see the benefit. It's REALLY easy to type this:
    random.choice(list(myIterator))

If we expected to WANT to pass iterators to random.choice
a lot, then it might be a good idea anyway. But I wouldn't
expect this. Why? random.choice is not a place where we
expect to probably use only the first few items and therefore
want an iterator so we don't have to generate them all --
on expectation we expect to use fully 1/2 the items. Worse,
we probably need to generate ALL of them in order to count
them. Basically, what we need is a sequence, so why not
require the explicit conversion.

It gets worse when you consider that one important feature
of iterators is that they may NOT have a length! Infinitely
producing iterators will be less common, but certainly not
uncommon.

I agree we should modify a number of std library functions
to use iterators, but random.choice seems to me to be a
very poor (excuse the pun!) choice.

-- Michael Chermside





More information about the Python-list mailing list