Two questions on lambda:

John Roth newsgroups at jhrothjr.com
Fri Jun 24 09:47:43 EDT 2005


"Xavier Décoret" <Xavier.Decoret at imag.fr> wrote in message 
news:d9gvl9$lo2$1 at trompette.imag.fr...
> Hi,
>
> I cannot find the way to do generic lambda functions using the lambda 
> syntax in python. I am probably missing a point.

You are. Lambda is restricted to a _single expression_.

Your first example is a statement, not an expression. Your
second example attempts to do an assignment in the body
of a lambda (although that's not what the code actually says)
and assignment is a statement, not an expression.

Lambda is intended for very simple, one line functions. It's
also likely to go away in Python 3.0. Python, for better or
worse, is a multi-paradigm language combining the object
and procedural paradigms. It is not, and it is not intended
to be, a functional style language.

John Roth

>
> For example, the code
>
> # f = lambda : print "hello"
> # f()
>
> does not compile, although:
>
> # def f():
> # print "hello"
> # f()
>
> does compile. Is there a particular syntax for lambda that I am missing or 
> is it simply limited and I cannot do what I want with lambda.
>
> In the same spirit, how can I do to compute intermediary values in the 
> body of a lambda function. Let's say (dummy example):
>
> f = lambda x : y=x*x,y+y
>
>
> In languages like Caml, you can do:
>
> let f = function x -> let y=x*x in y+y;;
>
> Does the lambda : syntax in python allow for the same kind of constructs?
>
> Thanks. 




More information about the Python-list mailing list