lambda fctions & local variables

Fredrik Lundh effbot at telia.com
Mon Feb 28 08:23:57 EST 2000


Frantisek Kvapil wrote:
> I have some matching function:
> def match(pattern,value):
>     return len(pattern)==len(value)
>
> and some finding function:
> def find(pattern,list):
>     return filter(lambda x: match(pattern,x),list)
>
> but the problem is that the lambda function don't know
> local variable pattern,
>
> so may you be so nice to tell me how to make it works
> (still using lambda functions if possible)

use explicit binding:

    return filter(lambda x, p=pattern: match(p, x), list)

also see:

http://www.python.org/doc/FAQ.html#4.5
http://www.python.org/doc/FAQ.html#6.10

</F>





More information about the Python-list mailing list