a break for comprehensions
Cliff Crawford
cjc26 at nospam.cornell.edu
Wed Aug 8 20:06:59 EDT 2001
* Tom Good <Tom_Good1 at excite.com> menulis:
|
| You've inspired me to start writing a list-like wrapper for
| generators. Here is what I have so far. It combines some list syntax
| with lazy evaluation, and supports (some) slice operations on
| unbounded generators.
| [snip]
|
| def main():
| def gen():
| x = 0
| while 1:
| x += 1
| yield x
|
| glist = GenList(gen())
| print glist[4]
| print glist[:10]
This is neat :) It looks like your class will work with any iterator,
not just generators. So you could use it with, say, file objects:
file = GenList(iter(open('file.txt')))
print file[4]
print file[3:17]
# etc.
Also, I would propose adding
def __add__(iter1, iter2):
def f():
for val in iter1:
yield val
for val in iter2:
yield val
return GenList(f)
Then you can do stuff like the following, which simulates the Unix 'cat'
command (warning: untested):
files = reduce(lambda x, y: x+y, GenList(iter(open(sys.argv[1:]))))
for line in files:
print line,
--
Cliff Crawford http://www.sowrong.org/
"Today's Goths, who generally tend to be in their teens and 20s,
have nothing to do with the Germanic Visigoths of Europe in the
third and fourth centuries A.D." -- Jimmy Stewart
More information about the Python-list
mailing list