<div dir="auto"><div class="gmail_quote" dir="auto"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br><br>
<br>
A with-statement is great for when you care about the <br>
implementation details. Somebody has to care about the process of <br>
opening a file, reading from it and closing it. But when you *don't* <br>
care about those implementation details, a simple interface like a <br>
read() function is superior to a with-statement, *or* a with-expression, <br>
which shoves those details in your face.<br></blockquote></div><div dir="auto"><br></div><div dir="auto">Totally agree. In the case where you care about the implementation details. You can explain to a beginner:</div><div dir="auto"><br></div><div dir="auto">something = (f.read() with open('...') as f)</div><div dir="auto"><br></div><div dir="auto">Is another way to write:</div><div dir="auto"><br></div><div dir="auto">with open('filename') as f:</div><div dir="auto">    something = f.read()</div><div dir="auto"><br></div><div dir="auto">Exactly like the ternary if.</div><div dir="auto"><br></div><div dir="auto">And I agree that because with is often relevant for "implementation details" or beginning use as a "enter.. exit' statement (like a cpp destructor), it matches more imperative programming.</div><div dir="auto"><br></div><div dir="auto">with stack.push_matrix(Scale(4)):</div><div dir="auto">    triangle = draw_triangle_using(stack)</div><div dir="auto"><br></div><div dir="auto">Is used for it's "enter/close" functionality, but can still be used like:</div><div dir="auto"><br></div><div dir="auto">triangle = (draw_triangle_using(stack) with stack.push_matrix(Scale(4)) as NotUsed)</div><div dir="auto"><br></div><div dir="auto">But the "as close" is useless here.</div><div dir="auto"><br></div><div class="gmail_quote" dir="auto"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
> and in most cases when <br>
> reading the code you then have to look at the function to see if <br>
> anything else is done there.<br>
<br>
I doubt that many people really spend a lot of time digging into <br>
functions to see whether they do more than what they say. Unless and <br>
until I ran into unexpected problems, I'd be no more inclined to look <br>
into a function called "read" than I would be to do the same to len or <br>
math.sin. I'm sure it does what it says it does.<br></blockquote></div><div dir="auto"><br></div><div dir="auto">Fair point, the "def" statements generally are passed when read so they don't really count as overhead.</div></div>