473,549 Members | 2,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

better lambda support in the future?

I'm wondering why python still has limited lambda support. What's
stopping the developers of python to support more lisp-like lambda function?
Jul 18 '05 #1
31 1609
Lambda functions will become obsolette in the nearest future. This is
the PLAN.

Jul 18 '05 #2
Jason Zheng wrote:
I'm wondering why python still has limited lambda support. What's
stopping the developers of python to support more lisp-like lambda
function?


This comes up every few weeks on the list. If you haven't already,
check the archives in Google for 'anonymous def' or 'anonymous
function'. The usual response to this question is something along the
lines of "if it's good enough to create a function for, it's good enough
to name".

One of the other big reasons for this type of proposal never making it
far is that, so far, no one has found a syntax that everyone (or even
most people) like. You end up with things like:

x = func or lambda x, y:
# body of lambda func
# indented here

or

x = func or lambda x, y:
# body of lambda func
# indented here

or

x = func or lambda x, y {
# body of lamda func indented
# however you want...
}

or any thousand other varieties.

Even if you could settle the syntax issue, once you've decided that you
really do need a true block in an anonymous function, you're not really
saving much space by not declaring it:

def f(*args):
# body line 1
# body line 2
# ...
# body line N
x = func or f

v.s.

x = func or lambda *args:
# body line 1
# body line 2
# ...
# body line N

so, you save a single line, which is only 1 Nth of the size of your
function (where N is the number of lines in your function body). For a
single or double line function, these savings may be more substantial,
but chances are if your function is only one or two lines, it can be
written as an expression anyway (e.g. using LCs or GEs)...
Do check the archives -- there are a lot of discussions on this topic,
but hopefully the brief commentary above will give you something of an
overview.

Steve
Jul 18 '05 #3
Steven Bethard wrote:
Even if you could settle the syntax issue, once you've decided that you really do need a true
block in an anonymous function, you're not really saving much space by not declaring it:

def f(*args):
# body line 1
# body line 2
# ...
# body line N
x = func or f

v.s.

x = func or lambda *args:
# body line 1
# body line 2
# ...
# body line N


you meant:

def x(*args):
# body line 1
# body line 2
# ...
# body line N

v.s.

x = func or lambda *args:
# body line 1
# body line 2
# ...
# body line N

right?

</F>

Jul 18 '05 #4
Fredrik Lundh wrote:
Steven Bethard wrote:

Even if you could settle the syntax issue, once you've decided that you really do need a true
block in an anonymous function, you're not really saving much space by not declaring it:

def f(*args):
# body line 1
# body line 2
# ...
# body line N
x = func or f

v.s.

x = func or lambda *args:
# body line 1
# body line 2
# ...
# body line N

you meant:

def x(*args):
# body line 1
# body line 2
# ...
# body line N

v.s.

x = func or lambda *args:
# body line 1
# body line 2
# ...
# body line N

right?


You're welcome to name the function whatever you want -- notice in my
example that the function is used in the statement:

x = func or f

If you'd prefer the statement to read:

x = func or x

that's also fine. Depends on what exactly 'x' is, and whether or not it
really makes sense for the function I called 'f' to have the same name
as the variable called 'x'. It certainly may, but since I wasn't giving
real code, I didn't want to commit to that.

Perhaps a better example would have been something along the lines of:

dict(a=lambda *args:
# body 1
,
b=lambda *args:
# body 2
,
c=lambda *args:
# body 3
)[s](values)

v.s.

def a_func(*args):
# body 1
def b_func(*args):
# body 2
def c_func(*args):
# body 3
dict(a=a_func, b=b_func, c=c_func)[s](values)

where it's clear that I'm trying to use lambdas where expressions are
required.

I assume that the point you were trying to make is that:

def f(*args):
return expr

is equivalent to

f = lambda *args: expr

?

Steve
Jul 18 '05 #5
True enough, but suppose you want a hash of anonymous functions as
opposed to just a lexical? This is where lambas are nice to have.
Totally agreed about a small use here and there, but they do have some
use in dispatch tables, as they are a lot easier to read sometimes
than very long case statements. Of course, this would require
multi-line lambdas to exist...

--Michael
I assume that the point you were trying to make is that:

def f(*args):
return expr

is equivalent to

f = lambda *args: expr

?

Steve
--
http://mail.python.org/mailman/listinfo/python-list

Jul 18 '05 #6
Steven Bethard wrote:
You're welcome to name the function whatever you want -- notice in my example that the function is
used in the statement:

x = func or f

If you'd prefer the statement to read:

x = func or x

that's also fine. Depends on what exactly 'x' is, and whether or not it really makes sense for
the function I called 'f' to have the same name as the variable called 'x'. It certainly may, but
since I wasn't giving real code, I didn't want to commit to that.
if you write "def f", "f" is a variable, just like "x". "def" is an assignment
statement.

I'm not sure what "func" is supposed to be in your examples...
I assume that the point you were trying to make is that:

def f(*args):
return expr

is equivalent to

f = lambda *args: expr

?

def f(): .... return 1+2
.... g = lambda: 1 + 2
type(f) <type 'function'> type(g) <type 'function'>
import dis
dis.dis(f) 2 0 LOAD_CONST 1 (1)
3 LOAD_CONST 2 (2)
6 BINARY_ADD
7 RETURN_VALUE dis.dis(g) 1 0 LOAD_CONST 1 (1)
3 LOAD_CONST 2 (2)
6 BINARY_ADD
7 RETURN_VALUE
dir(f) ['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__', '__ge
tattribute__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__r
educe__', '__reduce_ex__' , '__repr__', '__setattr__', '__str__', 'func_closure',
'func_code', 'func_defaults' , 'func_dict', 'func_doc', 'func_globals', 'func_na
me'] dir(g) ['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__', '__ge
tattribute__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__r
educe__', '__reduce_ex__' , '__repr__', '__setattr__', '__str__', 'func_closure',
'func_code', 'func_defaults' , 'func_dict', 'func_doc', 'func_globals', 'func_na
me']
f.func_name 'f' g.func_name

'<lambda>'

</F>

Jul 18 '05 #7
Fredrik Lundh wrote:
I'm not sure what "func" is supposed to be in your examples...


Just an extra variable used to make sure that the lambda was being used
in a context (i.e. in an expression) where a simple def wouldn't
suffice. If the example is confusing, consider the dict example
instead. It makes it clearer that the lambdas are being used in places
where an expression is required.

Steve
Jul 18 '05 #8
Michael DeHaan wrote:
True enough, but suppose you want a hash of anonymous functions as
opposed to just a lexical?
I've seen at least one reasonable example of this kind of thing:

http://mail.python.org/pipermail/pyt...er/245432.html

Though I haven't yet seen an example that actually required lambdas with
blocks...
Totally agreed about a small use here and there, but they do have some
use in dispatch tables, as they are a lot easier to read sometimes
than very long case statements. Of course, this would require
multi-line lambdas to exist...


Certainly in the example above, I'd be willing to agree that the lambdas
are at least as readable as a buch of def's above would have been. I'm
not sure if multi-line lambdas would be as readable though... It all
depends on what syntax you write them in -- you can see the struggle I
went through in my other message... Where do I put the commas in a
dict? Can't be at the end of the lambda or they turn the last
expression into a tuple... I resorted to putting them on a separate
line, but of course there are other solutions.
If you have a good example of where you'd like to use multi-line
lambdas, in, say, a dispatch table, I'd like to take a look at how you'd
like to write them. I'm not yet convinced that there really is a
readable way to write such things...

Steve
Jul 18 '05 #9
Steven Bethard wrote:
Jason Zheng wrote:
I'm wondering why python still has limited lambda support. What's
stopping the developers of python to support more lisp-like lambda
function?

This comes up every few weeks on the list. If you haven't already,
check the archives in Google for 'anonymous def' or 'anonymous
function'. The usual response to this question is something along the
lines of "if it's good enough to create a function for, it's good enough
to name".


The true beauty of lambda function is not the convenience of creating
functions without naming them. Lambda constructs truly enables
higher-order function. For example, I can create a function A that
returns a function B that does something interesting according to the
arguments that I pass to function A.
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
18831
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it...
4
1704
by: Michael Foord | last post by:
I'm starting to read through the developer works article on Functional Programming - http://www-106.ibm.com/developerworks/library/l-prog.html It starts by giving some basic examples using lambda. What I'm wondering is what's the actual difference between these two forms ? pr = lambda s:s *and* def pr(s): return s
181
8695
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.
267
10573
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at http://www.artima.com/weblogs/viewpost.jsp?thread=147358
5
2150
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
26
2686
by: brenocon | last post by:
Hi all -- Compared to the Python I know and love, Ruby isn't quite the same. However, it has at least one terrific feature: "blocks". Whereas in Python a "block" is just several lines of locally-scoped-together code, in Ruby a "block" defines a closure (anonymous function). To avoid confusion let's call them Ruby block-closures. I see...
4
3148
by: Jerzie.Klenchier | last post by:
Hi all, I'm having some difficulty in getting the following piece of code to compile: #include <iostream> #include <deque> #include <algorithm> #include <boost/tuple/tuple.hpp> #include <boost/lambda/lambda.hpp>
0
7520
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, 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...
1
7470
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...
0
7809
tracyyun
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...
0
6043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1941
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
1
1059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.