<div class="gmail_quote">On Fri, Nov 16, 2012 at 1:25 AM, Andrew Barnert <span dir="ltr"><<a href="mailto:abarnert@yahoo.com" target="_blank">abarnert@yahoo.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
My use case was at the top of my first email:<br>
<br>
upperlines = (line.upper() for line in file with open(path, 'r') as file)<br></blockquote><div><br>And it's *awful*. If the only thing on the RHS of a simple assignment statement is a lambda or generator expression, that code should almost always be rewritten with def as a matter of style, regardless of other considerations.<br>
<br>However, I realised there's a more serious problem with your idea: the outermost clause in a list comprehension or generator expression is evaluated immediately and passed as an argument to the inner scope that implements the loop, so you have an unresolved sequencing problem between the evaluation of that argument and the evaluation of the context manager. If you want the context manager inside the generator, you *can't* reference the name bound in the as clause in the outermost iterable.<br>
<br>Cheers,<br>Nick.<br></div></div><br>-- <br>Nick Coghlan | <a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a> | Brisbane, Australia<br>