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. 6 1617
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.writ elines("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.zoerho ff(AT)gmail.com
zoerhoff(AT)fre eshell.org
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.
"Xavier Décoret" <Xa************ @imag.fr> wrote in message
news:d9******** **@trompette.im ag.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.
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.
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
"Xavier Décoret" <Xa************ @imag.fr> wrote in message
news:d9******** **@trompette.im ag.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 = 50, width = 50)
|
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 are constructed on the fly.
Now my problem is lazy evaluation. Or at least I
think it is. :-)
|
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 suddenly become harder
than they need to be.
An example of what I mean is a quick script I wrote for doing certain
actions based on a regexp, which I will simlify in this instance to
make the pertanant points more relevent.
|
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 of the ones I
looked, classified by how I would rewrite them (if I could):
* Rewritable as def statements (<name> = lambda <args>: <expr> usage)
These are lambdas used when a lambda wasn't needed -- an anonymous
function was created with...
|
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
He says he's going to dispose of map, filter, reduce and lambda. He's
going to give us product, any and all, though, which is nice of him.
| |
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 I applied myself to this problem and come up
with this off-the-wall proposal for you people. Perhaps this idea has
been proposed before, I don't know.
The solutions I have seen all assume that the lambda must be completely
inlined within the...
|
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 bit
complicated so any explanation would be appreciated.
Thanks
|
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 to catch up again
with Python.
I am now appearing for Job Interviews these days and I am wondering if
anybody of you appeared for a Python Interview. Can you please share
the questions you were asked. That will be great help to me.
|
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
boost::lambda::lambda_
functor_base<boost::lambda::other_action<boost::lambda::ifthenelsereturn_action>
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |