<div dir="auto">This brings the discussion of variable assignement in Expression. F<span style="font-family:sans-serif">unctional programming community seems to be more interested in python.</span><div dir="auto"><div dir="auto"><br></div><div dir="auto">lines = (f.readlines() with open('hello') as f)</div><div dir="auto">digit = (int('hello') except ValueError: 5)</div><div dir="auto">value = (x+y**2 where x,y = (2,4))</div><div dir="auto">values = [x+y**2 for x in range(5) for y in range(7)]</div><div dir="auto"><span style="font-family:sans-serif">values = [x+y**2 for x,y in product (range(5), range(7))]</span><br></div><div dir="auto">y = 5 if condition else 2</div><div dir="auto">y = (lambda x: x+2)(x=5)</div><div dir="auto"><br></div><div dir="auto">vs</div><div dir="auto"><br></div><div dir="auto">with open('hello') as f:</div><div dir="auto">    lines = f.readlines()</div><div dir="auto">del f  # f is leaked !</div><div dir="auto"><br></div><div dir="auto">x,y = 2,4</div><div dir="auto">value = x+y**2</div><div dir="auto">del x, y  # x,y are leaked !</div><div dir="auto"><br></div><div dir="auto">try:</div><div dir="auto">    digit = (int('hello')</div><div dir="auto">except ValueError:</div><div dir="auto">    digit = 5</div><div dir="auto"><br></div><div dir="auto">if condition:</div><div dir="auto">    y = 5</div><div dir="auto">else:</div><div dir="auto">    y = 2</div><div dir="auto"><br></div><div dir="auto">def f(x):</div><div dir="auto">    return x+2</div><div dir="auto">y = f(x=2)</div><div dir="auto">del f  # we want an anonymous function !</div><div dir="auto"><br></div><div dir="auto">Those "oneliners" is not only the will to be quicker in interactive mode, it's the way functional programming Thinks.</div><div dir="auto"><br></div><div dir="auto">If we add one, it's Logical to add the others to be consistent.</div><div dir="auto"><br></div><div dir="auto">Of course, one can always write functions like read_text but the ide of those construction is like the lambda, we want anonymous.</div></div></div><br><div class="gmail_quote"><div dir="ltr">Le jeu. 2 août 2018 à 13:56, Steven D'Aprano <<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Thu, Aug 02, 2018 at 11:35:11AM +0200, Ken Hilton wrote:<br>
<br>
> Where this would benefit: I think the major use case is `f.read() with<br>
> open('file') as f`.<br>
[...]<br>
> Therefore `f.read() with open('file') as f`, I think, would be much<br>
> welcomed as the best way to read a file in an expression.<br>
<br>
Perhaps so, but do we want to encourage that to the point of adding <br>
syntax to make it easier?<br>
<br>
f.read() is a (mild) code-smell. Unless your file is guaranteed to be <br>
smaller than the amount of free memory, it risks starting your OS <br>
thrashing. IMO that makes this an idiom only suitable for quick and <br>
dirty scripts where the the user knows the limitations of the script and <br>
can abide by them.<br>
<br>
(In these days of computers with multiple gigabytes of RAM, reading in <br>
an entire file is not as risky as it used to be. But on the other hand, <br>
in these days of terrabyte and even petabyte storage devices, there are <br>
more *really large* files too.)<br>
<br>
Don't get me wrong -- f.read() is not necessarily bad. I often write <br>
scripts that slurp in an entire file at once, but they're typically <br>
throw-away scripts, and I'm also the user of the script and I know not <br>
to call it on files above a certain size. (As Miss Piggy once said, <br>
"Never eat more in one sitting than you can lift.")<br>
<br>
But I'm not sure if this sort of thing is something we want to <br>
*encourage* rather than merely *allow*. Best practice for reading files <br>
is, after all, a with statement for a reason: we expect to read text <br>
files line by line, often wrapped in a try...except to handle <br>
exceptions.<br>
<br>
For your use-case, I suspect the best thing is a utility function:<br>
<br>
def read(name, size=-1, **kwargs):<br>
    with open(name, **kwargs) as f:<br>
        return f.read(size)<br>
<br>
Not every three-line function needs to be a built-in, let alone <br>
given syntax :-)<br>
<br>
<br>
> For those wondering about the scope semantics of the "as NAME", I think<br>
> they would be identical to the scope semantics of the "for" expression <br>
<br>
Its not really a "for expression" -- its a *comprehension*, which is <br>
much more than merely a for expression:<br>
<br>
    # this isn't legal<br>
    result = for x in seq<br>
<br>
One important difference is that unlike this proposed "with" expression, <br>
comprehensions have an obvious pair of delimiters which enclose the <br>
expression and give it a natural beginning and end. There's no need to <br>
memorise arcane operator precedences and parsing rules to work out where <br>
the "with...as" variable will be legal.<br>
<br>
Another important difference is that while there are good reasons for <br>
putting comprehension loop variables in their own sub-local scope, I <br>
don't see any such benefit to doing the same for this proposed with <br>
expression. I don't think we should encourage the proliferation of more <br>
and more layers of extra scopes. We already have six:<br>
<br>
sublocal (comprehensions)<br>
local<br>
nonlocal (enclosing functions)<br>
class<br>
global (module)<br>
builtins<br>
<br>
<br>
Let's be cautious about adding more varieties of sublocal scope.<br>
<br>
<br>
-- <br>
Steve<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank" rel="noreferrer">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div>