Python lambda as constant functor

lambda, python

Solution

This is a well-known gotcha - since you don't bind the current value of `p` when defining the `lambda`, when called it uses the current value of `p`. The solution is to use a named arg with default value to bind the "current" (definition time) value of `p`:

pairs = [('abc', 'xyz'), ('123', '987'), ('abc', '987')]

pairLambs = []

for p in pairs:
    pairLambs.append(lambda p=p: print(p))

pairLambs[0]()
pairLambs[1]()
pairLambs[2]()

Problem

I have code with labdas, I have checked each time the function object is created it is different (not reference to same object), but it doesn't work as I would expect it. Is there any way how to or I should use a functor to do it even if I have constant data which I insert into lambda`s body? ``` pairs = [('abc', 'xyz'), ('123', '987'), ('abc', '987')] pairLambs = [] for p in pairs: pairLambs.append(lambda: print(p)) pairLambs[0]() pairLambs[1]() pairLambs[2]() ``` outputs: ``` ('abc', '987') ('abc', '987') ('abc', '987') ``` but I need: ``` ('abc', 'xyz') ('123', '987') ('abc', '987') ```

Original source