Python错误:赋值前引用了局部变量
这是我的密码:Python错误:赋值前引用了局部变量,python,variables,compiler-errors,Python,Variables,Compiler Errors,这是我的密码: import time GLO = time.time() def Test(): print GLO temp = time.time(); print temp GLO = temp Test() 回溯(最近一次调用last):文件“test.py”,第11行,在 Test()文件“Test.py”,测试中的第6行 打印GLO UNBUNDLOCALERROR:分配前引用的局部变量“GLO” 当我添加GLO=temp时出错,如果我对其进
import time
GLO = time.time()
def Test():
print GLO
temp = time.time();
print temp
GLO = temp
Test()
回溯(最近一次调用last):文件“test.py”,第11行,在
Test()文件“Test.py”,测试中的第6行
打印GLO UNBUNDLOCALERROR:分配前引用的局部变量“GLO”
当我添加GLO=temp
时出错,如果我对其进行注释,该函数可以成功执行,为什么
如何设置
GLO=temp
?Python首先查看整个函数范围。因此,您的GLO
指的是下面的一个,而不是全局的。并参阅
或
Python首先查看整个函数范围。因此,您的
GLO
指的是下面的一个,而不是全局的。并参阅
或
在测试方法中,指定要引用全局声明的GLO变量,如下所示
def Test():
global GLO #tell python that you are refering to the global variable GLO declared earlier.
print GLO
temp = time.time();
print temp
GLO = temp
类似的问题可以在这里找到:
在测试方法中,指定要引用全局声明的GLO变量,如下所示
def Test():
global GLO #tell python that you are refering to the global variable GLO declared earlier.
print GLO
temp = time.time();
print temp
GLO = temp
类似的问题可以在这里找到:
def Test():
global GLO #tell python that you are refering to the global variable GLO declared earlier.
print GLO
temp = time.time();
print temp
GLO = temp