RES: lambda question

Alves, Carlos Alberto - Coelce calves at coelce.com.br
Mon Mar 11 13:18:43 EST 2002


Sorry, but your code doesn't work also. See below:

>>> def exe1(f,list):
	return map(lambda x,list=list:eval(f),list)
SyntaxError: local name 'f' in 'exe1' shadows use of 'f' as global in nested
scope 'lambda' (<pyshell#11>, line 1)
>>> 

By the way, where can I find the Python's LGB scoping rules. I would like to
read that.

> -----Mensagem original-----
> De: Fredrik Lundh [mailto:fredrik at pythonware.com]
> Enviada em: segunda-feira, 11 de março de 2002 14:59
> Para: python-list at python.org
> Assunto: Re: lambda question
> 
> 
> 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
> -->
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20020311/760d8f0c/attachment.html>


More information about the Python-list mailing list