[Python-ideas] should there be a difference between generators anditerators?

Raymond Hettinger python at rcn.com
Thu Sep 4 17:15:13 CEST 2008


From: "Bruce Frederiksen" <dangyogi at gmail.com>
> 1.  All of the itertools and map (and I've no doubt left some others out 
> here) be extended to propagate the extra generators methods: close, 
> throw and send.

Try wrapping each input iterator with an autocloser:

def closer(it):
    for elem in it:
        yield elem
    if hasattr(it, 'close'):
        it.close()

map(somefunc, closer(someiterable))


Raymond



More information about the Python-ideas mailing list