It's important to understand that when a variable becomes part of a closure it's the variable itself and not the value being included.
This means that all the closures created in the loop are using the very same variable f
, that at the end of the loop will contain the last value used inside the loop.
Because of how the language is defined however those captured variables are "readonly" in Python 2.x: any assignment makes a variable a local one unless it's declared global
(Python 3.x adds the nonlocal
keyword to allow writing to a local of the outer scope).
As Jochen Ritzel said in his answer the common idiom to avoid this variable capture and get instead value capture is to write
lambda f=f: os.startfile(f)
this works because default parameter values are evaluated at function creation time, and f
is not the external variable but a function parameter that will have the value you want as default (so this lambda is just a function with default values for parameters, not closing any lexical variable any more).