Ezio Melotti added the comment:

FTR one of the reason that led me to itercm() is:

with open(fname) as f:
    transformed = (transform(line) for line in f)
    filtered = (line for line in lines if filter(line))
    # ...

Now filtered must be completely consumed before leaving the body of the `with` 
otherwise this happens:

>>> with open(fname) as f:
...     transformed = (transform(line) for line in f)
...     filtered = (line for line in lines if filter(line))
... 
>>> # ...
>>> next(filtered)
ValueError: I/O operation on closed file.

With itercm() it's possible to do:

f = itercm(open(fname))
transformed = (transform(line) for line in f)
filtered = (line for line in lines if filter(line))
...
# someone consumes filtered down the line lazily
# and eventually the file gets closed

itercm() could also be used (abused?) where a regular `with` would do just fine 
to save one extra line and indentation level (at the cost of an extra import), 
e.g.:

def lazy_cat(fnames):
    for fname in fnames:
        yield from itercm(open(fname))

instead of: 

def lazy_cat(fnames):
    for fname in fnames:
        with open(fname) as f:
            yield from f

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue25014>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to