以下是一个多线程的python代码片段,其中的x,l都是全局变量,但在threadcode()函数中只声明了global x没有global l。完整的代码是可以成功运行,但是把global x注释掉后就会报错。这是为什么,Lock对象比较特殊吗?
[python] view plain copy
import threading, time, sys
x = 50
l = 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()
......
推广一下,就成了在什么情况下必须使用global声明全局变量的问题。这段代码不用在函数中声明global x也可以打印出x的值[python] view plain copy
x = 20
def getx():
print x
getx()
在segmentfault上提问,得到了这样的回答[plain] view plain copy
对于Python2而言,对于一个全局变量,你的函数里如果只使用到了它的值,而没有对其赋值(指a = XXX这种写法)的话,就不需要声明global。相反,如果你对其赋了值的话,那么你就需要声明global。声明global的话,就表示你是在向一个全局变量赋值,而不是在向一个局部变量赋值。
[python] view plain copy
x = 20
def getx():
x = 50
print x
getx()#print 50
[python] view plain copy
class A:
def __init__(self, x):
self.x = x
def setx(self, x):
self.x = x
a = A(50)
def func():
a.setx(20)
print a.x
func()#print 20