itertools candidate: warehouse()

Alex Martelli aleaxit at yahoo.com
Tue Oct 19 21:30:31 CEST 2004


Peter Otten <__peter__ at web.de> wrote:
   ...
> >>iavailable = chain(stock, starmap(random.random, repeat(())))
   ...
> No, you cannot pass a callable to repeat() and have it called. In the above
> line repeat(()) yields the same empty tuple ad infinitum. The trick is that
> starmap() calls random.random() with that empty tuple as the argument list,

Stylistically, I prefer
    iter(random.random, None)
using the 2-args form of the built-in iter, to 
    itertools.starmap(random.random, itertools.repeat(()))

However, itertools IS a speed demon...:

kallisti:~/cb alex$ python -m timeit -s 'import random, itertools as it'
\     > 'list(it.islice(iter(random.random, None), 666))'
1000 loops, best of 3: 884 usec per loop

kallisti:~/cb alex$ python -m timeit -s 'import random, itertools as it'
\     > 'list(it.islice(it.starmap(random.random, it.repeat(())), 666))'
1000 loops, best of 3: 407 usec per loop


Alex



More information about the Python-list mailing list