首页 > 一个关于python闭包的问题,内部函数可以获取外部函数的变量吗?

一个关于python闭包的问题,内部函数可以获取外部函数的变量吗?

def lazy_sum(*args):
    ax = 0
    def sum():
        for n in args:
            ax = ax + n
        return ax
    return sum
    
f = lazy_sum(1, 3, 5, 7, 9)
print f()

为什么这时会报错ax=0未声明,而这样:

def test():
    x = 1
    def child():
        print 'x+x=',x+x
        return x+x
    x = x + child()
    return x

f = test()
print f

就没问题呢?

刚刚又试了一下,第一段程序改成这样就可以了:

def lazy_sum(*args):
    ax = 0
    def sum():
        for n in args:
            c = ax + n
        return c
    return sum

f = lazy_sum(1, 3, 5, 7, 9)
print f()

我在想,是不是内部函数可以获得外部函数的变量,但是却不能修改它?


def lazy_sum(*args):
    ax = 0
    def sum():
        for n in args:
            ax = ax + n
        return ax
    return sum
    
f = lazy_sum(1, 3, 5, 7, 9)
print f()

ax = ax + n 中的第一个 ax= 定义了 sum 中的一个局部变量,遮盖掉了 lazy_sum 中的局部变量定义 ax = 0

ax = ax + n 中的第二个 ax 在还没定义的时候就参与了运算,导致未定义错误

只要 sum 中的 ax 不遮盖掉 lazy_sum 中的 ax 就可以了:

def lazy_sum(*args):
    ax = [0]
    def sum():
        for n in args:
            ax[0]  = ax[0] + n
        return ax[0]
    return sum

f = lazy_sum(1, 3, 5, 7, 9)
print f()

加上global ax可以解决问题。下面来分析一下原因。

在第一段代码中,ax = ax + n这是一个赋值表达式,我们知道,在Python中变量是不需要提前声明的,一个变量第一次赋值时就相当于声明了这个变量。所以这句代码的效果是声明了一个局部变量ax,同时又要将它自己赋值给它,这是不行的!因为这时候它还没有初始化(也就是还没被赋值)!

所以这就造成了你所说的内部函数可以获得外部函数的变量,但是却不能修改它的问题,而global就是为了解决这个问题才被引入的。

个人觉得这是Python设计的很不好的地方,这会导致闭包使用起来很奇怪,而且global也会导致程序的封装性受到破坏


sum的第一行加上:

global ax
【热门文章】
【热门文章】