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

Ethan Furman ethan at stoneleaf.us
Tue Mar 8 14:51:33 EST 2016


On 03/08/2016 10:34 AM, Chris Angelico wrote:
> 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.

So?  Enhance the idea to be allowing

(expr) as (name) anywhere:

   if (file.read() as data):
       # process data

I'm cool with that.  :)

--
~Ethan~



More information about the Python-ideas mailing list