Python函数全局变量?我知道我应该避免使用全局变量,因为这样的混淆,但是如果我要使用它们,下面是使用它们的有效方法吗?(我试图调用在单独函数中创建的变量的全局副本。)x = "somevalue"def func_A ():
global x # Do things to x
return xdef func_B():
x=func_A()
# Do things
return x
func_A()func_B()第二个函数使用的‘x’值与‘func_a’使用和修改的‘x’全局副本的值相同吗?定义后调用函数时,顺序重要吗?
3 回答

慕侠2389804
TA贡献1719条经验 获得超6个赞
global
global
.
global
global
d = {}l = []o = type("object", (object,), {})()def valid(): # these are all valid without declaring any names global! d[0] = 1 # changes what's in d, but d still points to the same object d[0] += 1 # ditto d.clear() # ditto! d is now empty but it`s still the same object! l.append(0) # l is still the same list but has an additional member o.test = 1 # creating new attribute on o, but o is still the same object
添加回答
举报
0/150
提交
取消