<div dir="ltr">This map-then-filter shortcoming is one of the most common warts I encounter with python syntax, and I would absolutely love an improvement here.<div><br></div><div>I think ChrisA is closest with:</div><div><br></div><div>> <span style="font-size:12.8px">I'm really not liking the proposed syntax, but maybe there's an </span><span style="font-size:12.8px">alternative. My first thought on reading this proposal is: "Ah, it's </span><span style="font-size:12.8px">like SQL's 'HAVING' keyword". The trouble is that SQL can identify its </span><span style="font-size:12.8px">columns, but Python doesn't have an easy syntax for "the thing you're </span><span style="font-size:12.8px">about to return". Something like:</span></div>><br style="font-size:12.8px"><span style="font-size:12.8px">> [abs(x) for x in numbers having _ > 5]</span><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">But I wonder, why not just:</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">[abs(x) for x in numbers if _ > 5]</span><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Where _ refers to the return expression within a comprehension, and is interpreted specially in this context?</span></div><div><span style="font-size:12.8px"><br></span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Mar 8, 2016 at 7:20 AM, Chris Angelico <span dir="ltr"><<a href="mailto:rosuav@gmail.com" target="_blank">rosuav@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Wed, Mar 9, 2016 at 1:59 AM, Émanuel Barry <<a href="mailto:vgr255@live.ca">vgr255@live.ca</a>> wrote:<br>
> I’m going be confused by what sort of name binding it does. A quick glance<br>
> at the keywords list tells me that no currently-existing keyword is “the<br>
> obvious choice” for this purpose. I don’t think such a small niche warrants<br>
> a new keyword, either.<br>
<br>
</span>Maybe a pseudo-keyword would be sufficient - comprehension/genexp<br>
syntax is pretty solidly specified, so it's less likely to break stuff<br>
than in general syntax.<br>
<br>
I'm really not liking the proposed syntax, but maybe there's an<br>
alternative. My first thought on reading this proposal is: "Ah, it's<br>
like SQL's 'HAVING' keyword". The trouble is that SQL can identify its<br>
columns, but Python doesn't have an easy syntax for "the thing you're<br>
about to return". Something like:<br>
<br>
[abs(x) for x in numbers having _ > 5]<br>
<br>
The semantics would be equivalent to:<br>
def <listcomp>():<br>
result = []<br>
for x in numbers:<br>
_ = abs(x)<br>
if _ > 5: result.append(_)<br>
return result<br>
<br>
A 'having' clause (and, by the way, I'm not too enthused about the<br>
name, but I'm using it as a placeholder because of SQL's use) would<br>
have to go after all 'for' and 'if' clauses, and would have access to<br>
one special name (maybe _ because of its use in interactive mode, or<br>
maybe something else) which holds the value that would be returned. A<br>
simple filter-out-the-false-ones could do this:<br>
<br>
[regex.match(line) for line in lines having _]<br>
<br>
Of course, people could just switch from 'if' to 'having' across the<br>
board, but this has the same consequences that it does in SQL: now<br>
*every* row has to be fully processed, only to be discarded at the<br>
last step. Using 'having' with no underscore would violate common<br>
sense and good style.<br>
<br>
ChrisA<br>
_______________________________________________<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></blockquote></div><br></div>