[Python-ideas] iter() on steroids
erez27 at gmail.com
Fri Apr 13 13:39:45 CEST 2007
I think it's a great idea. In a language where everything is meant to be
object-oriented and easy-to-use, it's funny that iterators (which are
becoming increasingly popular) have such a bumpy interface.
We wouldn't want to do "from numtools import nadd,nmul ; nadd(1,2)". The
claim that iterators aren't "being used often enough to justify your change"
is very disturbing considering that in future pythons most default functions
will return iterators if possible (such as key()/items()/values() of dict).
The WILL be used more than files, and it would be foolish to force the user
to do "from filetools import seek, tell, readlines"
The specifics of your proposal may need a little improvement here and there,
but the idea itself, IMHO, is very good and pythonic.
On 4/13/07, George Sakkis <george.sakkis at gmail.com> wrote:
> I proposed an (admittedly more controversial) version of this a few
> months back at the py3k list and the reaction was unexpectedly (IMO)
> negative or indifferent, so I'm wondering if things have changed a bit
> The proposal is to make the the builtin iter() return an object with
> an API that consists of (most) functions currently at itertools. In
> addition to saving one "from itertools import chain,islice,..." line
> in every other module I write these days, an extra bonus of the OO
> interface is that islice can be replaced with slice syntax and chain
> with '+' (and/or perhaps the "pipe" character '|'). As a (deliberately
> involved) example, consider this:
> # A composite iterator over two files specified as follows:
> # - each yielded line is right stripped.
> # - the first 3 lines of the first file are yielded.
> # - the first line of the second file is skipped and its next 4 lines
> are yielded
> # - empty lines (after the right stripping) are filtered out.
> # - the remaining lines are enumerated.
> f1,f2 = [iter(open(f)).map(str.rstrip) for f in 'foo.txt','bar.txt']
> for i,line in (f1[:3] + f2[1:5]).filter(None).enumerate():
> print i,line
> The equivalent itertools version is left as an exercise to the reader.
> This is actually backwards compatible and could even go in 2.x if
> accepted, but I'm focusing on py3K here.
> Comments ?
> PS: FYI, a proof of concept implementation is posted as a recipe at:
> Python-ideas mailing list
> Python-ideas at python.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas