标签:关于 引用 count int 函数 ret 误区 count() 函数返回
像这种内层函数引用了外层函数的变量(参数也算变量),然后返回内层函数的情况,称为闭包(Closure)。
闭包的特点是返回的函数还引用了外层函数的局部变量,所以,要正确使用闭包,就要确保引用的局部变量在函数返回后不能变。
def count():
    fs = []
    for i in range(1, 4):
        def test1(j):
            def test2():
                return j * j
            return test2
        fs.append(test1(i))
    return fs
[f1, f2, f3] = count()
print f1(), f2(), f3()
def count():
    fs = []
    for i in range(1, 4):
        def f():
             return i*i
        fs.append(f)
    return fs
f1, f2, f3 = count()
标签:关于 引用 count int 函数 ret 误区 count() 函数返回
原文地址:http://www.cnblogs.com/wskgjmhh/p/7534823.html