473,387 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Two questions on lambda:

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
6 1588
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
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
"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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andrew Koenig | last post by:
from Tkinter import * class Application(Frame): def setcolor(self): self = "blue" def createWidgets(self): self.b1 = Button(self, bg = "red", command = self.setcolor) self.b1.place(height...
53
by: Oliver Fromme | last post by:
Hi, I'm trying to write a Python function that parses an expression and builds a function tree from it (recursively). During parsing, lambda functions for the the terms and sub-expressions...
63
by: Stephen Thorne | last post by:
Hi guys, I'm a little worried about the expected disappearance of lambda in python3000. I've had my brain badly broken by functional programming in the past, and I would hate to see things...
26
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some...
181
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 ...
23
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes...
5
by: Octal | last post by:
How does the lambda library actually works. How does it know how to evaluate _1, how does it recognize _1 as a placeholder, how does it then calculate _1+_2, or _1+2 etc. The source files seem a...
8
by: Krypto | last post by:
Hi, I have used Python for a couple of projects last year and I found it extremely useful. I could write two middle size projects in 2-3 months (part time). Right now I am a bit rusty and trying...
1
by: Tim H | last post by:
Compiling with g++ 4: This line: if_then_else_return(_1 == 0, 64, _1) When called with a bignum class as an argument yields: /usr/include/boost/lambda/if.hpp: In member function 'RET...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.