Closure in python?
问题 When I run this code, I get this result: 15 15 I expect the output should be 15 17 but it is not. The question is: why? def make_adder_and_setter(x): def setter(n): x = n return (lambda y: x + y, setter) myadder, mysetter = make_adder_and_setter(5) print myadder(10) mysetter(7) print myadder(10) 回答1: Python 2.x has a syntax limitation that doesn't allow to capture a variable in read/write. The reason is that if a variable is assigned in a function there are only two possibilities: the