This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ezio.melotti
Recipients ezio.melotti, ncoghlan, rhettinger
Date 2015-09-06.13:14:21
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1441545261.7.0.192244822349.issue25014@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2015-09-06 13:14:21ezio.melottisetrecipients: + ezio.melotti, rhettinger, ncoghlan
2015-09-06 13:14:21ezio.melottisetmessageid: <1441545261.7.0.192244822349.issue25014@psf.upfronthosting.co.za>
2015-09-06 13:14:21ezio.melottilinkissue25014 messages
2015-09-06 13:14:21ezio.melotticreate