闭包 学习讨论

def line_conf(a, b):

    i = a * b
    def line(x):
        i = i + x
        return i * x + b
    return line
 
line1 = line_conf(4, 5)
print line1(5)
 
在python2.x中报UnboundLocalError错误,原因应该是变量i是函数line_conf()的局部变量,函数调用结束之后也就清除了这个变量,而line1()中需要依赖变量i,才会报这个错误
posted on 2014-11-14 10:36  清明-心若淡定  阅读(197)  评论(0编辑  收藏  举报