List comprehension vs filter()

Mel mwilson at the-wire.com
Wed Apr 20 16:12:21 CEST 2011


Chris Angelico wrote:
> On Wed, Apr 20, 2011 at 5:16 PM, Tim Roberts <timr at probo.com> wrote:
>> You can solve this through the common lamba idiom of a closure:
>>
>> lst=filter(lambda x,posttype=posttype: x["type"].lower()==posttype,lst)
> 
> Seems a little odd, but sure. I guess this means that a function's
> default arguments are evaluated in the parent context, but the body is
> evaluated in its own context?

The operation of calling a function has to evaluate arguments provided in 
the caller's namespace and assign them to variables in the called function's 
namespace.  Yes.

	Mel.




More information about the Python-list mailing list