[Tutor] reading lines from a list of files

Peter Otten __peter__ at web.de
Tue May 12 20:08:17 CEST 2015


Oscar Benjamin wrote:

> A generator cannot guarantee that execution continues after a yield so
> any context manager used around a yield is dependent on __del__. I
> think a good rule of thumb is "don't yield from a with block".

Uh-oh, I am afraid I did this quite a few times. Most instances seem to be 
context managers though. Is something like

@contextmanager
def my_open(filename):
    if filename == "-":
        yield sys.stdin
    else:
        with open(filename) as f:
            yield f


OK?




More information about the Tutor mailing list