473,382 Members | 1,423 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,382 software developers and data experts.

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 2407
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.com" <levili...@gmail.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.com" <levili...@gmail.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.com 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.com" <le*******@gmail.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.org> 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*****@pythonware.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.org> 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.com" <levili...@gmail.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.com 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(lambda x=x: x+2)
print funcs[3]()
Try these w/o the default binding.

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

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

Similar topics

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...
6
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...
5
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,...
1
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
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...
9
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
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...
4
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) :...
3
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.