lambda
Quinn Dunkan
quinn at ngwee.ugcs.caltech.edu
Wed Mar 29 15:22:09 EST 2000
On 28 Mar 2000 22:37:53 -0800, Falknor <falknor at worldspy.net> wrote:
>Hmm, what exactly is the use of lambda? It just isn't clickin for some
>reason.... Are they just syntactical candy (such as the ternary operator ?:
>in C++ is syntactical candy..) or what. *ponders* Anyone who could explain
>it, and gimme a little example would have my thanx. :)
f = lambda x: x * 2
is the same as
def f(x):
return x * 2
In other words, lambda is just another way of writing `def' except that it
doesn't have to be named and you can only put an expression in it (return is
implicit).
It's intended to be a shorthand to write small functions easier, but that's
all it is: a shorthand. This is unlike, say, scheme, where it is the *only*
way to define functions. Remember, in python, a function is just another
type, like integers and strings and classes. Classes use Classname() as a
type constructor, strings use ""s, and functions use def and lambda.
It's useful for HOFs (higher-order functions) which want a function as an arg,
like map, filter, and reduce.
More information about the Python-list
mailing list