itertools candidate: warehouse()

Jack Diederich jack at performancedrivers.com
Tue Oct 19 15:54:31 EDT 2004


On Tue, Oct 19, 2004 at 09:30:31PM +0200, Alex Martelli wrote:
> 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(()))
>
> kallisti:~/cb alex$ python -m timeit -s 'import random, itertools as it'
\ > 'list(it.islice(it.starmap(random.random, it.repeat(())), 666))'

unrelated, I also often import itertools as 'it'.  It bothers me because
I do it frequently (grep says 25 of 97 modules).  Moving all of itertools into
builtins seems like overkill, but could we hang them off the 'iter' builtin?
Making the above:

> kallisti:~/cb alex$ python -m timeit -s 'import random'
\ > 'list(iter.islice(iter.starmap(random.random, iter.repeat(())), 666))'

My life would definitely get easier, no more assuming 'it' is imported and
then adding the import when I find out differently.

A quick dir() of iter shows no conflicts.
>>> dir(iter)
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__str__']


-Jack




More information about the Python-list mailing list