Comment by oulipo
14 days ago
I would have just done something like
``` def loop(): for number in range(10): fixed_number = number def inner(): return fixed_number yield inner ```
14 days ago
I would have just done something like
``` def loop(): for number in range(10): fixed_number = number def inner(): return fixed_number yield inner ```
Correctly formatted (two spaces preceding each line, one blank line before the first code line, no extra lines needed between code lines):
The output:
It doesn't work because of the way Python's variables are scoped. Your fixed_number variable is still shared across all instances of inner. Python doesn't have any sort of block scoping like you seem to think it has.