Python变量范围是特定于类型的?

时间:2015-08-19 12:51:17

标签: python scope wrapper

from functools import wraps
def mywrapper(func):
    variable1 = {}
    variable2 = 0
    @wraps(func)
    def newfunc(*args):
        if args in variable1:
            return variable1[args]
        else:
            variable1[args] = func(*args)
            variable2 = 1
            return variable1[args]
    return newfunc

如果我运行此代码,我收到错误消息“未分配本地变量 variable2 ”。但是,如果我将其更改为字典,它就像 variable1 一样正常。这让我对Python变量范围感到困惑----它是否依赖于类型?我使用的是2.7.6。

0 个答案:

没有答案