我有下面的代码片段:非绑定本地变量问题
def isolation_level(level):
def decorator(fn):
def recur(level, *args, **kwargs):
if connection.inside_block:
if connection.isolation_level < level:
raise IsolationLevelError(connection)
else:
fn(*args, **kwargs)
else:
connection.enter_block()
try:
connection.set_isolation_level(level)
fn(*args, **kwargs)
connection.commit()
except IsolationLevelError, e:
connection.rollback()
recur(e.level, *args, **kwargs)
finally:
connection.leave_block()
def newfn(*args, **kwargs):
if level is None: # <<<< ERROR MESSAGE HERE, Unbound local variable `level`
if len(args):
if hasattr(args[0], 'isolation_level'):
level = args[0].isolation_level
elif kwargs.has_key('self'):
if hasattr(kwargs['self'], 'isolation_level'):
level = kwargs.pop('self', 1)
if connection.is_dirty():
connection.commit()
recur(level, *args, **kwargs)
return newfn
return decorator
这真的不要紧它做什么,但是我将它张贴在其原来的形式,因为我无法重现情况简单些。
问题是,当我打电话isolation_level(1)(some_func)(some, args, here)
我得到Unbound local variable
例外21行(在列表上标记)。我不明白为什么。我尝试重新创建功能和函数调用的相同结构,这些调用不包含所有实现细节以找出错误。但是,我没有收到异常消息。例如,下面的工作:
def outer(x=None):
def outer2(y):
def inner(x, *args, **kwargs):
print x
print y
print args
print kwargs
def inner2(*args, **kwargs):
if x is None:
print "I'm confused"
inner(x, *args, **kwargs)
return inner2
return outer2
outer(1)(2)(3, z=4)
打印:
1
2
(3,)
{'z': 4}
我在想什么?
编辑
好了,问题是,在第一个版本我实际执行给变量赋值。 Python检测到并因此假定该变量是本地的。
错误信息可以复制几行,如果知道是什么原因,很容易。见http://codepad.org/nI0vCx4L – delnan 2011-04-29 22:36:55
非常感谢,我现在明白了。 – julkiewicz 2011-04-29 22:44:18
@delnan:导致错误的最短代码是在内函数中使用'x = x' :) – 2011-04-29 22:49:25