lambda question

Fredrik Lundh fredrik at pythonware.com
Mon Mar 11 12:59:25 EST 2002


lambda questionAlves, Carlos Alberto wrote:

> first code:
>         f='x**3+2*x'
>         list=[0,1,2,0]
>         map(lambda x:eval(f),list)
>
> second code:
>
>         def exe1(f,list):
>                 return map(lambda x:eval(f),list)
>
> Can someone explain to me why the first code works while
> second doesn't.

because you're running under Python 2.1 or earlier, and
haven't read up on Python's LGB scoping rules?

if so, the following should do the trick:

    def exe1(f,list):
        return map(lambda x,list=list:eval(f),list)

</F>

<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list