Securing a future for anonymous functions in Python
jeff at ccvcorp.com
Thu Jan 6 19:23:36 EST 2005
Alan Gauld wrote:
> Can I ask what the objection to lambda is?
> 1) Is it the syntax?
> 2) Is it the limitation to a single expression?
> 3) Is it the word itself?
> I can sympathise with 1 and 2 but the 3rd seems strange since a
> lambda is a well defined name for an anonymous function used in
> several programming languages and originating in lambda calculus
> in math. Lambda therefore seems like a pefectly good name to
I think that the real objection is a little bit of 1), and something
that's kinda close to 2), but has nothing to do with 3).
The issue isn't that lambdas are bad because they're limited to a
single expression. The issue is that they're an awkward special case
of a function, which was added to the language to mollify
functional-programming advocates but which GvR never felt really "fit"
into Python. Other, more pythonic functional-programming features
have since been added (like list comprehensions and iterators).
It seems to me that in other, less-dynamic languages, lambdas are
significantly different from functions in that lambdas can be created
at runtime. In Python, *all* functions are created at runtime, and
new ones can be defined at any point in execution, so lambdas don't
get that advantage. Thus, their advantages are limited to the fact
that they're anonymous (but names are treated differently in Python
than in most other languages, so this is of marginal utility), and
that they can be created inline. This last bit makes them suitable
for creating quick closures (wrapping a function and tweaking its
parameters/return values) and for creating a delayed-execution object
(e.g. callbacks), so there's a lot of pressure to keep them, but
they're still a special case, and "special cases aren't special enough
to break the rules".
More information about the Python-list