By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,035 Members | 1,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

Two questions on lambda:

P: n/a
Hi,

I cannot find the way to do generic lambda functions using the lambda
syntax in python. I am probably missing a point.

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.
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 6/24/05, Xavier Décoret <Xa************@imag.fr> wrote:

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.
lambda calls can only include functions; print is a statement, not a
function. Try this instead:

import sys
f = lambda : sys.stdout.writelines("Hello")
f()

However, if you're going to be binding the function to a name, there
is no need to use lambda at all; just def a function and be done with
it.
In the same spirit, how can I do to compute intermediary values in the
body of a lambda function. Let's say (dummy example):


I leave this to someone more expert than I.

--
Kristian

kristian.zoerhoff(AT)gmail.com
zoerhoff(AT)freeshell.org
Jul 19 '05 #2

P: n/a
Xavier Décoret wrote:
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.


Lambda is deliberately limited. Just define a function.

The only downside to defining a function is that you have to think of a
name for it, but that name is simply a variable like any other and can be
rebound at will.

Any attempt to write an expression such as:

f = lambda x : y=x*x,y+y

should instantly set off lots of alarm bells in your mind. Defining a
lambda simply to assign it to a name is obviously wrong: it should be a
function definition instead.
Jul 19 '05 #3

P: n/a
"Xavier Décoret" <Xa************@imag.fr> wrote in message
news:d9**********@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.


Jul 19 '05 #4

P: n/a
hello,

On Fri, 24 Jun 2005 14:48:16 +0200, Xavier Décoret wrote:
Hi,

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?
You can define another lambda function with a default value for the y
parameter. For instance:

f = lambda x: (lambda y=x*x: y+y)()


Thanks.

Jul 19 '05 #5

P: n/a
def PRINT(x): print x
f = lambda: PRINT("hello")

###################################

def let(x,y):
globals()[x] = y
return True

f = lambda x: let('y',x*x) and y+y

Jul 19 '05 #6

P: n/a

"Xavier Décoret" <Xa************@imag.fr> wrote in message
news:d9**********@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.


Thinking of lambda args: expression
as more or less abbreviating def <lambda>(args): return expression
should fill you in.

Two differences:
1. the first is an expression and can be used as such within other
expressions;
the second a statement that stands alone.
2. the first will compile (with proper substitutions for 'args' and
'expression');
the second will not, because '<lambda>' is not a legal name.

However, in CPython, '<lambda>' *is* the .func_name attribute of
lambda-defined functions (so they are not quite anonymous;-).

Terry J. Reedy

Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.