Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

Steven D'Aprano steve at pearwood.info
Tue Mar 25 07:28:04 CET 2014


On Mon, 24 Mar 2014 19:16:15 -0500, Mark H Harris wrote:

> On 3/24/14 7:11 PM, Chris Angelico wrote:
>> On Tue, Mar 25, 2014 at 10:56 AM, Mark H Harris<harrismh777 at gmail.com> 
>> wrote:
>>> What is needed is the explicit closure "grab" recommended by ChrisA.
>>
>> Which does work. You do know why, right?
> 
> Sure.  ... but again, that's not the point. The point is NOT can you
> explain why it works, the point is that as a lambda construct it is NOT
> clear why it works, and because the construct does not match what lambda
> users might expect (naturally) there are *constant* questions about it.
> 
> So, again, I'll restate that the community might consider (over time)
> whether the confusion created by lambda in python


Functions created with def work exactly the same way. If you are 
surprised by lambda's binding behaviour, and you replace the lambda with 
def, you will still be confused. You'll have gone from a nice, concise 
expression to a bulky statement, and be no better off. You'll be worse 
off.


-- 
Steven



More information about the Python-list mailing list