[Python-ideas] FW: Map-then-filter in comprehensions

Chris Angelico rosuav at gmail.com
Tue Mar 8 13:34:15 EST 2016


On Wed, Mar 9, 2016 at 5:32 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
> On 03/08/2016 10:27 AM, Chris Angelico wrote:
>
>> Implement that, and people will ask why they can't then unroll that:
>>
>> def <listcomp>():
>>      result = []
>>      for x, y in some_iterable:
>>          if x+y as z > 10: # SyntaxError
>>              result.append(z)
>>      return z
>
>
> Seriously?
>
> def blah():
>     result = []
>     for x, y in some_iterable:
>         z = x + y
>         if z > 10:
>             result.append(z)
>     return result  # not z
>
> That doesn't seem too difficult.  ;)

Ah yes, but that's not how you've written it in the comprehension. You
wrote it with 'as'. Believe you me, people WILL expect that outside of
comprehensions. Otherwise, you have to explain why a name binding is
legal in a condition in a comprehension, but not in any other
expression.

ChrisA


More information about the Python-ideas mailing list