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

Tim Chase python.list at tim.thechases.com
Thu Jan 10 13:39:16 EST 2008


> 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






More information about the Python-list mailing list