这段代码不用在函数中声明global x就可以打印出x的值x = 20def getx(): print xgetx()以下是一个多线程的python代码片段,其中的x,l都是全局变量,但在threadcode()函数中只声明了global x没有global l。完整的代码是可以成功运行,但是把global x注释掉后就会报错。请问这是为什么,Lock对象比较特殊吗?import threading, time, sysx = 50l = threading.Lock()
def threadcode(): global x
l.acquire()
print 'Thread %s invoked.' % threading.currentThread().getName() try:
print 'Thread %s running.' % threading.currentThread().getName() x = x + 50
print 'Thread %s set x to %d.' % \
(threading.currentThread().getName(), x) finally:
l.release()
...
2 回答
德玛西亚99
TA贡献1770条经验 获得超3个赞
对于Python2而言,对于一个全局变量,你的函数里如果只使用到了它的值,而没有对其赋值(指a = XXX
这种写法)的话,就不需要声明global。相反,如果你对其赋了值的话,那么你就需要声明global
。声明global
的话,就表示你是在向一个全局变量赋值,而不是在向一个局部变量赋值。
肥皂起泡泡
TA贡献1829条经验 获得超6个赞
Python的作用域解析是基于叫做LEGB(Local(本地),Enclosing(封闭),Global(全局),Built-in(内置))的规则进行操作的。看下面的例子
>>> x = 10>>> def foo():... x += 1... print x...>>> foo()Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in foo UnboundLocalError: local variable 'x' referenced before assignment
这是因为,在一个作用域里面给一个变量赋值的时候,Python自动认为这个变量是这个作用域的本地变量,并屏蔽作用域外的同名的变量。很多时候可能在一个函数里添加一个赋值的语句会让你从前本来工作的代码得到一个UnboundLocalError
。
下面的是文档中的解释:
This is because when you make an assignment to a variable in a scope, that variable becomes local to that scope and shadows any similarly named variable in the outer scope.
在使用列表(lists)的时候,很容易就触发这种错误。看下面这个例子:
>>> lst = [1, 2, 3]>>> def foo1():... lst.append(5) # 这没有问题......>>> foo1()>>> lst[1, 2, 3, 5] >>> lst = [1, 2, 3]>>> def foo2():... lst += [5] # ... 这就有问题了!...>>> foo2()Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in foo UnboundLocalError: local variable 'lst' referenced before assignment
添加回答
举报
0/150
提交
取消