Framework for a beginner

Terry Reedy tjreedy at udel.edu
Thu Apr 19 14:08:35 EDT 2012


On 4/19/2012 8:12 AM, lkcl luke wrote:

> you don't *have* to use lambdas with map and reduce,
> you just have touse a function,
 > where a lambda happens to be a nameless function.

Abbreviated statements like the above sometimes lead people to think 
that there is more difference between def functions and lambda functions 
than there is, at least in Python.

The word 'lambda' is used ambiguously by various people for either the 
expression or the resulting object -- or perhaps even for both. You 
clearly mean the function object above, but other are not so clear.

The word 'nameless' is also ambiguous in that it could refer to the 
syntax construct, a name attribute, or a namespace binding. Lambda 
expressions are nameless, but the resulting function objects are not. 
Lambda expressions and def statements both produce function objects with 
a .__name__ attribute. For lambda functions, .__name__ is always 
'<lambda>'. So to be exact, a lambda function, resulting from a lambda 
expression, is a function with the default name '<lambda>'.

As for name binding: a lambda function can be explicitly bound to a name 
in any namespace, just like any other object. The automatic local 
namespace binding of a def function can be explicitly undone, just like 
any other binding. So any function, regardless of creation syntax, can 
be named or not in any namespace, just like any other object.

-- 
Terry Jan Reedy




More information about the Python-list mailing list