I recently implemented a function factory that didn't scope a variable the way I expected.
I found a solution, but I don't know why it works.
So although I don't strictly need a solution to this problem, I've posted this question in the hope of learning something about variable scope.
Problematic Code
# Create function list
func_list = []
for i in [1, 2]:
def _func():
print("%i" % i)
func_list.append(_func)
# Execute
for f in func_list:
f()
Gives the same ouptut; always the last loop element:
2
2
Solution
# Create function list
def make_func(i):
def _func():
print("%i" % i)
return _func
func_list = []
for i in [1, 2]:
func_list.append(make_func(i))
# Execute
for f in func_list:
f()
Gives what I expected all along:
1
2
Why? so my questions are:
- Why doesn't the first method work?
- How is the working implementation functionally different to the first?