473,585 Members | 2,657 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is "lambda x=x : ... " ?

I'm reading this page: http://www.ps.uni-sb.de/~duchier/pyt...inuations.html
and I've found a strange usage of lambda:

############### #####
Now, CPS would transform the baz function above into:

def baz(x,y,c):
mul(2,x,lambda v,y=y,c=c: add(v,y,c))

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

What does "y=y" and "c=c" mean in the lambda function?
I thought it bounds the outer variables, so I experimented a little
bit:

############### ##
x = 3
y = lambda x=x : x+10

print y(2)
############### ###

It prints 12, so it doesn't bind the variable in the outer scope.
Jan 10 '08 #1
9 2417
I've figured it out, it is default argument.
print y()
gives 13 as result.

It's a bit evil though.
I hope this post will be useful some newbie like i'm now someday :)

On Jan 10, 7:25 pm, "zsl...@gmail.c om" <levili...@gmai l.comwrote:
I'm reading this page:http://www.ps.uni-sb.de/~duchier/pyt...inuations.html
and I've found a strange usage of lambda:

############### #####
Now, CPS would transform the baz function above into:

def baz(x,y,c):
mul(2,x,lambda v,y=y,c=c: add(v,y,c))

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

What does "y=y" and "c=c" mean in the lambda function?
I thought it bounds the outer variables, so I experimented a little
bit:

############### ##
x = 3
y = lambda x=x : x+10

print y(2)
############### ###

It prints 12, so it doesn't bind the variable in the outer scope.
Jan 10 '08 #2

You're talking about syntax from the bad old days
when the scope rules were different.

If not too archeological for your tastes, download
and boot a 1.5 and see what happens.

Less empirically, here're some key references:
http://www.python.org/doc/2.2.3/whatsnew/node9.html
http://www.python.org/dev/peps/pep-0227/

The change came in 2.2 with from __future__ support
in 2.1.

Kirby
4D
On Jan 10, 11:25 am, "zsl...@gmail.c om" <levili...@gmai l.comwrote:
I'm reading this page:http://www.ps.uni-sb.de/~duchier/pyt...inuations.html
and I've found a strange usage of lambda:

############### #####
Now, CPS would transform the baz function above into:

def baz(x,y,c):
mul(2,x,lambda v,y=y,c=c: add(v,y,c))

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

What does "y=y" and "c=c" mean in the lambda function?
I thought it bounds the outer variables, so I experimented a little
bit:

############### ##
x = 3
y = lambda x=x : x+10

print y(2)
############### ###

It prints 12, so it doesn't bind the variable in the outer scope.
Jan 10 '08 #3
What does "y=y" and "c=c" mean in the lambda function?

the same thing it does in a function definition:

def myfunct(a, b=42, y=3.141):
pass
############### ##
x = 3
y = lambda x=x : x+10

print y(2)
############### ###

It prints 12, so it doesn't bind the variable in the outer scope.
You're mostly correct, as it does pull it out of the local
context. Try

x = 3
y = lambda x=x: x+10
print y(2)
print y()

to get "12" then "13" back.

-tkc

Jan 10 '08 #4
zs****@gmail.co m wrote:
############### #####
Now, CPS would transform the baz function above into:

def baz(x,y,c):
mul(2,x,lambda v,y=y,c=c: add(v,y,c))

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

What does "y=y" and "c=c" mean in the lambda function?
they bind the argument "y" to the *object* currently referred to by the
outer "y" variable. for example,

y = 10
f = lambda y=y: return y
y = 11

calling f() will return 10 no matter what the outer "y" is set to.

in contrast, if you do

y = 10
f = lambda: y
y = 11

calling f() will return whatever "y" is set to at the time of the call.

or in other words, default arguments bind to values, free variables bind
to names.
I thought it bounds the outer variables, so I experimented a little
bit:

############### ##
x = 3
y = lambda x=x : x+10

print y(2)
############### ###

It prints 12, so it doesn't bind the variable in the outer scope.
it does, but you're overriding the bound value by passing in a value. try:

x = 3
y = lambda x=x : x+10
y()
x = 10
y()

instead.

</F>

Jan 10 '08 #5
On Thu, 10 Jan 2008 10:25:27 -0800 (PST) "zs****@gmail.c om" <le*******@gmai l.comwrote:
I'm reading this page: http://www.ps.uni-sb.de/~duchier/pyt...inuations.html
and I've found a strange usage of lambda:

############### #####
Now, CPS would transform the baz function above into:

def baz(x,y,c):
mul(2,x,lambda v,y=y,c=c: add(v,y,c))

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

What does "y=y" and "c=c" mean in the lambda function?
Older versions of python didn't make variables in an outer scope
visible in the inner scope. This was the standard idiom to work
around that.

<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.
Jan 10 '08 #6
Mike Meyer wrote:
>What does "y=y" and "c=c" mean in the lambda function?

Older versions of python didn't make variables in an outer scope
visible in the inner scope. This was the standard idiom to work
around that.
lexically scoped free variables and object binding are two different
things, and have different semantics. the former does not always
replace the latter.

</F>

Jan 10 '08 #7
On Thu, 10 Jan 2008 19:59:23 +0100 Fredrik Lundh <fr*****@python ware.comwrote:
Mike Meyer wrote:
What does "y=y" and "c=c" mean in the lambda function?
Older versions of python didn't make variables in an outer scope
visible in the inner scope. This was the standard idiom to work
around that.
lexically scoped free variables and object binding are two different
things, and have different semantics. the former does not always
replace the latter.
And?

<mike

--
Mike Meyer <mw*@mired.or g> http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.
Jan 10 '08 #8
On Jan 10, 12:36 pm, "zsl...@gmail.c om" <levili...@gmai l.comwrote:
I've figured it out, it is default argument.
print y()
gives 13 as result.

It's a bit evil though.
Why? It's the same syntax as with functions:

x=3
def y(x=x):
return x+10

print y(2) # prints 12
print y() # prints 13
Jan 10 '08 #9
zs****@gmail.co m wrote:
I'm reading this page: http://www.ps.uni-sb.de/~duchier/pyt...inuations.html
and I've found a strange usage of lambda:

############### #####
Now, CPS would transform the baz function above into:

def baz(x,y,c):
mul(2,x,lambda v,y=y,c=c: add(v,y,c))

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

What does "y=y" and "c=c" mean in the lambda function?
I thought it bounds the outer variables, so I experimented a little
bit:

############### ##
x = 3
y = lambda x=x : x+10

print y(2)
############### ###

It prints 12, so it doesn't bind the variable in the outer scope.
Primary use:
funcs = [lambda x=x: x+2 for x in range(10)]
print funcs[3]()

_or_

funcs = []
for x in range(10):
funcs.append(la mbda x=x: x+2)
print funcs[3]()
Try these w/o the default binding.

--Scott David Daniels
Sc***********@A cm.Org
Jan 11 '08 #10

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

Similar topics

53
3644
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. :-)
6
16932
by: Ken Varn | last post by:
Sometimes when I try to close my managed C++ application, the following dialog displays in Win 2000 Pro: The title of the dialog is "Server Busy". The message is "This action cannot be completed because the other program is busy. Choose 'Switch to' to activate the busy program and correct the problem." I don't know why this is...
5
2756
by: Jarek | last post by:
Hi all! I'm optimizing my C++ multi-threaded application (linux). My application consumes huge amout of memory from unknown reason. There are no memory leaks, or other allocation bugs, application works well, but on startup it has about 200mb (VmSize). How can I investigate what function/class/other takes so much memory ? I tried to verify...
1
2222
by: blue | last post by:
Looking at code from the following: http://www.gamedev.net/reference/programming/features/enginuity2/page3.asp I'm confused though... CMMPointer(T *o) { obj=0; *this=0;
5
2067
by: junky_fellow | last post by:
Each time i submit some pattern to "google", it shows search took 0.XX seconds for exploring millions of web pages. When i look for efficient ways of searching a string, they always say compare your string with the strings in the file one by one. if there are millions of web pages then these algorithms would take considerable amount of...
9
4017
by: Dullme | last post by:
i can hardly know what is the code for this. I have researched palindromes, but unfortunately, I only have read about integers not for characters..I need some help..
5
3918
by: cowgurl art | last post by:
Hey there. I will not pretend to know more than I do...I'm just getting my feet wet with Dreamweaver and MySql databases. What I'm trying to do is create a form that will allow members to submit an audio file (mp3 or .wav) from a website. The form will insert the record into the database IF I can get it configured correctly. Here's the...
4
2817
by: CBFalconer | last post by:
regis wrote: Why don't you read the documentation (or source) from whomever supplied you that (non-standard) function? -- : Chuck F (cbfalconer at maineline dot net) : <http://cbfalconer.home.att.net> Try the download section.
3
221
by: Marc Gravell | last post by:
A lambda expression is a short form to write a delegate In /this/ case (LINQ-to-Objects): yes - but it could equally be compiled to an Expression, which is very, very different. A lambda *statement*, on the other hand, is always a delegate. Marc
0
7908
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...
0
7836
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...
0
8336
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7950
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
8212
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...
1
5710
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
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2343
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
1447
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.