Two questions on lambda:

  • Thread starter =?ISO-8859-1?Q?Xavier_D=E9coret?=
  • Start date
?

=?ISO-8859-1?Q?Xavier_D=E9coret?=

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.
 
K

Kristian Zoerhoff

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.
 
D

Duncan Booth

Xavier said:
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.
 
J

John Roth

Xavier Décoret said:
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
 
C

Christophe Delord

hello,

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)()
 
S

Sean McIlroy

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
 
T

Terry Reedy

Xavier Décoret said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,260
Messages
2,571,301
Members
47,944
Latest member
LillianPra

Latest Threads

Top