闭包 学习讨论

时间:2021-12-17 03:23:54

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,才会报这个错误