itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

Raymond Hettinger vze4rx4y at verizon.net
Tue Mar 29 03:55:10 EST 2005


[Jack Diederich]
>  > itertools to iter transition, huh?  I slipped that one in, I mentioned
>  > it to Raymond at PyCon and he didn't flinch.  It would be nice not to
>  > have to sprinkle 'import itertools as it' in code.  iter could also
>  > become a type wrapper instead of a function, so an iter instance could
>  > be a wrapper that figures out whether to call .next or __getitem__
>  > depending on it's argument.
>  > for item in iter(mylist).imap:
>  >   print item
>  > or
>  > for item in iter.imap(mylist):
>  >   print item

[Steven Bethard]
> Very cool idea.  I think the transition from
>      itertools.XXX(iterable, *args, **kwargs)
> to
>      iter.XXX(iterable, *args, **kwargs)
> ought to be pretty easy.

Just to make sure you guys can live with your proposed syntax, trying using it
for a month or so and report back on whether the experience was pleasant.  Try
dropping the following into your setup.py

def wrapiter():
    import __builtin__, itertools
    orig = __builtin__.iter
    def iter(*args):
        return orig(*args)
    for name in ('__doc__', '__name__'):
        setattr(iter, name, getattr(orig, name))
    vars(iter).update(vars(itertools))
    __builtin__.iter = iter
wrapiter()

If the experience works out, then all you're left with is the trivial matter of
convincing Guido that function attributes are a sure cure for the burden of
typing import statements.


Raymond Hettinger





More information about the Python-list mailing list