[Tutor] filtering within a function.

Tesla Coil tescoil@irtc.net
Mon, 12 Mar 2001 15:37:17 -0500


On 12 Mar 2001, Michael P. Reilly replied:
>>> How about:
>>> uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> def unstray(listinput, f=lambda x: x in uppercase):
>>>  l = filter(f, listinput)
>>>  # replace the contents of listinput with the result of the filtering
>>>  listinput[:] = l
>>>
>>> But then, maybe I'm missing the point. :)
>> 
>> Not that I can see.  Is this bad form?:
>> 
>> def unstray(listinput):
>>   f=lambda x: x in uppercase
>>   l = filter(f, listinput)
>>   listinput[:] = l
>
> About the only thing is that putting the lambda in the 
> function declaration does not recreate the lambda function
> every time and also keeps it out of the global namespace.

"Keeps it out of the global namespace," I got.  "Does not
recreate the lambda function every time," I need explained
--not yet a black belt in the lambda attack like Pedro. ;)