这是我的代码:
import time
GLO = time.time()
def Test():
print GLO
temp = time.time();
print temp
GLO = temp
Test()
Traceback (most recent call last): File "test.py", line 11, in Test() File "test.py", line 6, in Test print GLO UnboundLocalError: local variable 'GLO' referenced before assignment
添加GLO = temp
时发生错误,如果我对其进行注释,则函数可以成功执行,为什么?
如何设置GLO = temp
?
在测试方法中指定要引用全局声明的GLO变量,如下所示
类似的问题可以在这里找到: Using a global variable within a method
Python首先查看整个函数范围。所以你的
GLO
指的是下面的那个,而不是全局的。并引用LEGB rule。或者
相关问题 更多 >
编程相关推荐