<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Aug 2, 2018 at 5:35 AM, Ken Hilton <span dir="ltr"><<a href="mailto:kenlhilton@gmail.com" target="_blank">kenlhilton@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">Hi, I don't know if someone has already suggested this before, but here goes:</div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">With expressions allow using the enter/exit semantics of the with statement inside an expression context. Examples:</div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">    contents = f.read() with open('file') as f #the most obvious one</div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">    multiplecontents = [f.read() with open(name) as f for name in names] #reading multiple files</div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">I don't know if it's worth making the "as NAME" part of the with mandatory in an expression - is this a valid use case?</div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">    data = database.selectrows() with threadlock</div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">Where this would benefit: I think the major use case is `f.read() with open('file') as f`. Previous documentation has suggested `open('file').read()` and rely on garbage collection; as the disadvantages of that became obvious, it transitioned to a method that couldn't be done in an expression:</div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">    with open('file') as f:</div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">        contents = f.read()</div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">Therefore `f.read() with open('file') as f`, I think, would be much welcomed as the best way to read a file in an expression.</div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">For those wondering about the scope semantics of the "as NAME", I think they would be identical to the scope semantics of the "for" expression - i.e. these are legal:</div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">    contents = f.read() with open('file') as f</div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">    grid = [[i] * 4 for i in range(4)]</div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">But these are not:</div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">    contents = f.read() with open('file') as f</div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">    f.seek(0)</div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">    grid = [[i] * 4 for i in range(4)]</div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">    grid[i][i] = 4</div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">Is this a good idea? Are there some subtleties I've failed to explain? Please let me know.</div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">Sharing,</div><div style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">Ken Hilton</div></div><br></blockquote></div></div><div class="gmail_extra"><br></div><div class="gmail_extra">If this is a common enough operation for you, it would be trivially easy to just write a function that does this.  There is already a module on pypi that has this function: read_and_close.<br></div></div>