我有这样的代码
global _portlist1
def Identify():
#do something
_portlist1=["a","b"]
def RunTest():
print _portlist1
#do something else
def run():
Identify()
RunTest()
在 RunTest() 中,_portlist1 是空的,但它是在 Identify() 中定义的。由于它是一个全局变量,在运行 Identify() 之后它的值不应该被改变
请您参考如下方法:
你需要这个:
def Identify():
global _portlist1
_portlist1 = ["a", "b"]
即在函数内部声明全局变量。否则,一个局部变量会隐藏它。
