question about what lamda does

nephish at xit.net nephish at xit.net
Thu Jul 20 09:53:13 EDT 2006


hey thanks for that last post, although some of it was a bit over my
head.
i think i am getting more of the differences here.

thanks again,
sk

danielx wrote:
> nephish at xit.net wrote:
> > Hey there,
> > i have been learning python for the past few months, but i can seem to
> > get what exactly a lamda is for. What would i use a lamda for that i
> > could not or would not use a def for ? Is there a notable difference ?
> > I only ask because i see it in code samples on the internet and in
> > books.
> >
> > thanks for any clarity
> >
> > sk
>
> hehe. Lambda's are kind of a sensative subject for pythoners who come
> from Lisp. Guido being more of a C guy doesn't really like them, and
> thought they should be removed in py3k. Last time I checked, he was
> reconsidering because of public outcry, presumably from the Lisp crowd.
>
> The standard reason for getting rid of it is "anywhere you need a
> lambda, you can use a def". In addition to what has been said here,
> there is another one small difference between lambda's and functions,
> which is that when you use def, the object gets a name:
>
> >>> def foo(): pass
> ...
> >>> foo
> <function foo at 0x009D8230>
> #             ^ foo knows its own name
> >>> bar
> <function foo at 0x009D8230>
> #             ^ see ;)
> >>>
>
> Whereas, a lambda has no name; it's "anonymous":
>
> >>> spam = lambda: 1
> >>> spam
> <function <lambda> at 0x009D80F0>
> #             ^ spam has an identity crisis ;)
> >>>
>
> Many people who do not come from Lisp do not understand what the use of
> a lambda is (and I have no idea what the purpose of having a name is).
> Even people who do question whether it belongs in Python. In Lisp,
> lambda's are the way things get done, because you can calculate
> anything using just defines and expressions. This style does not fit
> Python very well, since we do things using statements.
>
> Python's lambda really can't be as powerful as Lisp's because Python
> does not have expressions that do case analysis (this is not lambda's
> fault, of course ;). The reason is that you really want to put each
> case on its own set of lines. This enhances readability at the expense
> of terseness. Since Python's statements are terminated by a newline, it
> would be rather awkward to have a kind of expression where good style
> calls for it to be spread out accross multiple lines.
>
> You can try to simulate these kinds expressions using into a list or
> dictionary, but this becomes rather messy. I think the only way to get
> this done properly is to use eval. For example:
>
> def recursiveFunction(args):
>   ...  # do stuff...
>   choices = { True:"0", False:"recurisveFunction(newArgs)" }
>   return eval( choices[predicate] )
>
> The reason that you need eval is that you want to prevent any cases
> from being executed until you decide which one you want. This stay of
> execution is accomplished by wrapping quotes around our expressions.
> This example illustrates why we really need this kind of behavior,
> because without it, we would fall into an infinite loop. Even if it
> were safe to evaluate all cases, it's a big waste of time to do so.
>
> Lastly, I think there is also a performance concern for certain uses of
> lambda (correct me if I'm wrong). Say you have an expression with a
> lambda in it where you could have used a def. Every time you evaluate
> that expression, you have to construct a new lambda object, which takes
> time. If you had used a def instead, you could hav avoided having to
> construct multiple times.




More information about the Python-list mailing list