<div dir="ltr">Some time ago I was trying to solve the same issue but using a new keyword "where", and I thought that new keyword is too much for just list comprehension filtering, so I've made it something like assignments in expresion, eg.<div><br></div><div>(x+y)**2 + (x-y)**2 where x=1, y=2</div><div><br></div><div>So for list comprehension I can write:</div><div><br></div><div>[stripped for line in lines if stripped where stripped=line.strip()]</div><div><br></div><div>or:<br></div><div><br></div><div>result = map(f, objs) where f=lambda x: x.return_something()</div><div><br></div><div>or:</div><div><br></div><div>it = iter(lines)</div><div>while len(line) > 4 where line=next(it, '').strip():</div><div>    print(line)</div><div><br></div><div>or:</div><div><br></div><div>lambda x, y: (</div><div>    0 if z == 0 else</div><div>    1 if z > 0 else</div><div>    -1) where z = x + y</div><div><br></div><div>or even:</div><div><br></div><div>lambda something: d where (d, _)=something, d['a']=1</div><div><br></div><div>I even implemented it:</div><div><a href="https://github.com/thektulu/cpython/commit/9e669d63d292a639eb6ba2ecea3ed2c0c23f2636">https://github.com/thektulu/cpython/commit/9e669d63d292a639eb6ba2ecea3ed2c0c23f2636</a><br></div><div><br></div><div>and it works nicely. I was thinking to reuse "with [expr] as [var]" but I also <span style="font-size:12.8px">don't like idea of context sensitive </span><span style="font-size:12.8px">semantics, and I even thought that maybe someone, someday would want to write "content = fp.read() with open('foo.txt') as fp"...</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">The "where" keyword is from guards pattern in Haskell :)</span></div></div><div class="gmail_extra"><br><div class="gmail_quote">2016-03-10 17:53 GMT+01:00 Pavol Lisy <span dir="ltr"><<a href="mailto:pavol.lisy@gmail.com" target="_blank">pavol.lisy@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">2016-03-09 6:16 GMT+01:00, Sjoerd Job Postmus <<a href="mailto:sjoerdjob@sjec.nl">sjoerdjob@sjec.nl</a>>:<br>
[...]<br>
<div><div class="h5">> Trying to word it in such a way:<br>
><br>
> "... considering each of the `for` or `if` clauses a block, nesting from<br>
> left to right, and evaluating the expression to produce an element each<br>
> time the innermost block is reached. The `with expr as target` should be<br>
> considered equivalent to `target = expr`.<br>
><br>
> (And here we already see the downside of this idea). Normally a<br>
> comprehension of the form<br>
><br>
>     (expr1 for target1 in expr2 for target2 in expr3 if expr4)<br>
><br>
> boils down to<br>
><br>
>     for target1 in expr2:<br>
>         for target2 in expr3:<br>
>             if expr4:<br>
>                 yield expr1<br>
><br>
> The natural extension would be for<br>
><br>
>     (expr1 for target1 in expr2 with expr3 as target2 if expr4)<br>
><br>
> to reduce as follows.<br>
><br>
>     for target1 in expr2:<br>
>         with expr3 as target2:<br>
>             if expr4:<br>
>                 yield expr1<br>
><br>
> Instead, it becomes<br>
><br>
>     for target1 in expr2:<br>
>         target2 = expr3:<br>
>         if expr4:<br>
>             yield expr1<br>
><br>
> But of course we're not going to have context managers in<br>
> comprehensions, are we? So this inconsistency is somewhat forgiveable.<br>
<br>
</div></div>If we want variable assignment we have already "for var in [expr]" syntax.<br>
<br>
We could discuss if "with expr as var" syntax is more beautiful. (or<br>
if it not against There should be one-- and preferably only one<br>
--obvious way to do it.)<br>
<br>
But why omit context manager semantics in "with expr as var" assignment syntax?<br>
<br>
I personally don't like idea that semantics could be context sensitive<br>
in this way. (and we could already do pretty complex things in<br>
comprehension)<br>
<div class="HOEnZb"><div class="h5">_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="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" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</div></div></blockquote></div><br></div>