parameter undefined in procedure
Moshe Zadka
moshez at math.huji.ac.il
Fri Feb 25 15:31:41 EST 2000
On Fri, 25 Feb 2000, David Smith wrote:
> I have a module named test.py, which contains only the following
> definition:
>
> def bar(dictionary):
> list = ['alpha', 'bravo', 'charlie', 'delta', 'echo', 'foxtrot']
> return map( lambda(x): dictionary.get(x,"default"), list)
>
> I import it and run it, and get an error message as follows:
<a name error on dictionary>
When a Python function sees a variable, it looks in three places (in that
order) for the variable: the local namespace, the global namespace, and
the builtin namespace.
The "lambda" expression defines a function. Inside it, the only locals
are the parameters (since there can be no assignments inside a lambda),
so in your case the only local is "x". "dictionary" isn't a global,
and it isn't a builtin (thank God!) either. If you want your lambda
expression to see it, you have to explicitly define it:
return map(lambda x, dictionary=dictionary: dictionary.get(x, "default"),
list)
If you think this is god-awful ugly, you're right. Why not code it
as an explicit loop?
--
Moshe Zadka <mzadka at geocities.com>.
INTERNET: Learn what you know.
Share what you don't.
More information about the Python-list
mailing list