def make_functions():
    flist = []
    for i in [1, 2, 3]:
        def print_i():
            print(i)
        flist.append(print_i)
    return flist
functions = make_functions()
for f in functions:
    f()
The output of this code is:
3
3
3
Questions:
- Why does print_ionly capture the final value ofi?
- How can we modify this code to make it print 1, 2, 3?
One way to answer question #2 is as follows, but I'm wondering if there's a more elegant way that captures the value of i.
def make_functions():
    flist = []
    for i in [1, 2, 3]:
        def print_i(j):
            print(j)
        flist.append((print_i, i))
    return flist
functions = make_functions()
for f, i in functions:
    f(i)
 
     
    