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

Z

zslevi

I'm reading this page: http://www.ps.uni-sb.de/~duchier/python/continuations.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.
 
Z

zslevi

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 :)
 
T

Tim Chase

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
 
F

Fredrik Lundh

####################
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>
 
M

Mike Meyer

I'm reading this page: http://www.ps.uni-sb.de/~duchier/python/continuations.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
 
F

Fredrik Lundh

Mike said:
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>
 
M

Mike Meyer

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
 
F

Fredrik Lundh

Mike said:

and what? it's not the same thing. the "newer" idiom only replaces
the "older" idiom under certain circumstances (such as in the OP's first
example, but *not* in his second example).

</F>
 
D

Dustan

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
 
S

Scott David Daniels

I'm reading this page: http://www.ps.uni-sb.de/~duchier/python/continuations.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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,904
Messages
2,570,003
Members
46,359
Latest member
thejackson123

Latest Threads

Top