This question already has an answer here:
- Why can't Python increment variable in closure? 5 answers
- Read/Write Python Closures 7 answers
Python 2 doesn't support assignments to variables closed over by a nested function. The usual workaround is to put the value in a mutable container (e.g., a one-element list). Python 3 offers the
nonlocal keyword for this purpose.