lambda question

Fredrik Lundh fredrik at pythonware.com
Mon Mar 11 14:22:41 EST 2002


RES: lambda questionAlves, Carlos Alberto wrote:

> 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)

try this one instead:

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

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

the language reference, perhaps?

http://www.python.org/doc/2.1/ref/execframes.html

    "The _local namespace_ of an execution frame determines
    the default place where names are defined and searched.
    The _global namespace_ determines the place where names
    listed in global statements are defined and searched, and
    where names that are not bound anywhere in the current
    code block are searched."

    "When a global name is not found in the global namespace,
    it is searched in the built-in namespace (which is actually
    the global namespace of the module __builtin__ )."

(local, global, built-in -> LGB)

note that the scoping rules has been modified in Python 2.2:

    http://www.python.org/doc/2.2/ref/nested-scopes.html

under the revised scoping rules, your example should work as
originally written.

</F>





More information about the Python-list mailing list