[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

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

levilista@gmail.com

1/10/2008 6:25:00 PM

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

levilista@gmail.com

1/10/2008 6:37:00 PM

0

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

kirby.urner@gmail.com

1/10/2008 6:39:00 PM

0


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/...
http://www.python.org/dev/peps...

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

Tim Chase

1/10/2008 6:39:00 PM

0

> 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



Fredrik Lundh

1/10/2008 6:43:00 PM

0

zslevi@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>

Mike Meyer

1/10/2008 6:44:00 PM

0

On Thu, 10 Jan 2008 10:25:27 -0800 (PST) "zslevi@gmail.com" <levilista@gmail.com> wrote:

> I'm reading this page: http://www.ps.uni-sb.de/~duchier/python/continua...
> 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 <mwm@mired.org> http://www.mired.org/consu...
Independent Network/Unix/Perforce consultant, email for more information.

Fredrik Lundh

1/10/2008 6:59:00 PM

0

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>

Mike Meyer

1/10/2008 7:42:00 PM

0

On Thu, 10 Jan 2008 19:59:23 +0100 Fredrik Lundh <fredrik@pythonware.com> wrote:
> 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 <mwm@mired.org> http://www.mired.org/consu...
Independent Network/Unix/Perforce consultant, email for more information.

Fredrik Lundh

1/10/2008 7:52:00 PM

0

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?

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>

Dustan

1/10/2008 11:14:00 PM

0

On Jan 10, 12:36 pm, "zsl...@gmail.com" <levili...@gmail.com> wrote:
> 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

Scott David Daniels

1/11/2008 1:34:00 AM

0

zslevi@gmail.com wrote:
> I'm reading this page: http://www.ps.uni-sb.de/~duchier/python/continua...
> 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
Scott.Daniels@Acm.Org