views:

1063

answers:

3

I understand what are lambda functions in Python, but I can't find what is the meaning of "lambda binding" by searching the Python docs. A link to read about it would be great. A trivial explained example would be even better. Thank you.

+1  A: 

Where have you seen the phrase used?

"Binding" in Python generally refers to the process by which a variable name ends up pointing to a specific object, whether by assignment or parameter passing or some other means, e.g.:

a = dict(foo="bar", zip="zap", zig="zag") # binds a to a newly-created dict object
b = a # binds b to that same dictionary

def crunch(param):
  print param

crunch(a) # binds the parameter "param" in the function crunch to that same dict again

So I would guess that "lambda binding" refers to the process of binding a lambda function to a variable name, or maybe binding its named parameters to specific objects? There's a pretty good explanation of binding in the Language Reference, at http://docs.python.org/ref/naming.html

Dan
+7  A: 

First, a general definition: Lambda Binding of Arguments in Programs and Functions

When a program or function statement is executed, the current values of formal parameters are saved (on the stack) and within the scope of the statement, they are bound to the values of the actual arguments made in the call. When the statement is exited, the original values of those formal arguments are restored. This protocol is fully recursive. If within the body of a statement, something is done that causes the formal parameters to be bound again, to new values, the lambda-binding scheme guarantees that this will all happen in an orderly manner.

Now, there is an excellent python example in a discussion here:

"...there is only one binding for x: doing x = 7 just changes the value in the pre-existing binding. That's why

def foo(x): 
   a = lambda: x 
   x = 7 
   b = lambda: x 
   return a,b

returns two functions that both return 7; if there was a new binding after the x = 7, the functions would return different values [assuming you don't call foo(7), of course. Also assuming nested_scopes]...."

Swati
+2  A: 

I've never heard that term, but one explanation could be the "default parameter" hack used to assign a value directly to a lambda's parameter. Using Swati's example:

def foo(x): 
    a = lambda x=x: x 
    x = 7 
    b = lambda: x 
    return a,b

aa, bb = foo()
aa () # Prints 4
bb () # Prints 7
John Millikin