[Python-ideas] iter() on steroids (original) (raw)
George Sakkis george.sakkis at gmail.com
Fri Apr 13 06:39:41 CEST 2007
- Previous message: [Python-ideas] Ideas towards GIL removal
- Next message: [Python-ideas] iter() on steroids
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 since.
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 ?
George
PS: FYI, a proof of concept implementation is posted as a recipe at: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498272
- Previous message: [Python-ideas] Ideas towards GIL removal
- Next message: [Python-ideas] iter() on steroids
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]