<div dir="ltr"><div><div>Haskell has a feature like this in comprehensions where one may write: [r + 1| n <- [1..10], let r = n * 3, r `rem` 4 == 0]<br></div>Here we are sharing the definition of `r` in both the predicate and the value expresions, we can also use the name in different expressions in both contexts.<br><br></div>If we were to translate this to python syntax we could have something like: [r + 1 for n in range(1, 11) for n * 3 as r if r % 4 == 0]<br><div><div>There is no reason that the name binding needs to be a part of the predicate expression, they can just be seperate clauses. I think the `for expr as name` is nice because it matches the order that comprehensions over multiple iterators are evaluated like: `[n for n in ns for m in ms]`.<br><br></div><div>I personally just write `map` and `filter` and then have the control to make the correct function get called first but adding something like this might make me more inclined to use comprehensions for the simple cases.<br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Mar 8, 2016 at 1:34 PM, 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 5:32 AM, Ethan Furman <<a href="mailto:ethan@stoneleaf.us">ethan@stoneleaf.us</a>> wrote:<br>
> On 03/08/2016 10:27 AM, Chris Angelico wrote:<br>
><br>
>> Implement that, and people will ask why they can't then unroll that:<br>
>><br>
>> def <listcomp>():<br>
>>  Â  Â  result = []<br>
>>  Â  Â  for x, y in some_iterable:<br>
>>  Â  Â  Â  Â  if x+y as z > 10: # SyntaxError<br>
>>  Â  Â  Â  Â  Â  Â  result.append(z)<br>
>>  Â  Â  return z<br>
><br>
><br>
> Seriously?<br>
><br>
> def blah():<br>
>  Â  Â result = []<br>
>  Â  Â for x, y in some_iterable:<br>
>  Â  Â  Â  Â z = x + y<br>
>  Â  Â  Â  Â if z > 10:<br>
>  Â  Â  Â  Â  Â  Â result.append(z)<br>
>  Â  Â return result  # not z<br>
><br>
> That doesn't seem too difficult.  ;)<br>
<br>
</span>Ah yes, but that's not how you've written it in the comprehension. You<br>
wrote it with 'as'. Believe you me, people WILL expect that outside of<br>
comprehensions. Otherwise, you have to explain why a name binding is<br>
legal in a condition in a comprehension, but not in any other<br>
expression.<br>
<br>
ChrisA<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>