itertools candidate: warehouse()

Carlos Ribeiro carribeiro at gmail.com
Tue Oct 19 17:22:08 EDT 2004


On Tue, 19 Oct 2004 15:54:31 -0400, Jack Diederich
<jack at performancedrivers.com> wrote:
> 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__']

Just wondering, but maybe this idea could be generalized to reduce the
(ever growing) cluttering of the builtins namespace. I'm not sure
about the side effects though. The basic idea is that one would still
import the itertools module (under the name iter, in this example):

import iter

...and then it get both iter(), and all iter.<something> methods.

It's actually possible to do some similar things right now; for
example, the str builtin allows one to access all string methods. Is
there any 'showstopper' for this generalization? I don't know if a
module can have a __call__ method, but it seems to be something
interesting to explore...

Just curious, and sincerely, haven't checked it anywhere...


-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com



More information about the Python-list mailing list