@author jackzhenguo
@desc
@date 2019/2/20
关键词nonlocal
常用于函数嵌套中,声明变量i
为非局部变量;
如果不声明,i+=1
表明i
为函数wrapper
内的局部变量,因为在i+=1
引用(reference)时,i未被声明,所以会报unreferenced variable
的错误。
def excepter(f):
i = 0
t1 = time.time()
def wrapper():
try:
f()
except Exception as e:
nonlocal i
i += 1
print(f'{e.args[0]}: {i}')
t2 = time.time()
if i == n:
print(f'spending time:{round(t2-t1,2)}')
return wrapper