Aks*_*har 255 python global-variables
由于这样的混乱,我知道我应该首先避免使用全局变量,但如果我要使用它们,以下是使用它们的有效方法吗?(我试图调用在单独函数中创建的变量的全局副本.)
x = "somevalue"
def func_A ():
global x
# Do things to x
return x
def func_B():
x=func_A()
# Do things
return x
func_A()
func_B()
Run Code Online (Sandbox Code Playgroud)
请问__CODE__第二函数使用具有全球复制相同的值__CODE__是__CODE__使用和修改?在定义后调用函数时,命令有关系吗?
Lev*_*von 387
如果您只想访问全局变量,只需使用其名称即可.但是要更改其值,您需要使用global关键字.
例如
global someVar
someVar = 55
Run Code Online (Sandbox Code Playgroud)
这会将全局变量的值更改为55.否则,它只会将55分配给局部变量.
函数定义列表的顺序无关紧要(假设它们不以某种方式相互引用),它们被调用的顺序.
jdo*_*dot 105
在Python范围内,对尚未在该范围内声明的变量的任何赋值都会创建一个新的局部变量,除非该函数在函数的前面声明为引用带有关键字的全局范围变量global.
让我们看一下你的伪代码的修改版本,看看会发生什么:
# Here, we're creating a variable 'x', in the __main__ scope.
x = 'None!'
def func_A():
# The below declaration lets the function know that we
# mean the global 'x' when we refer to that variable, not
# any local one
global x
x = 'A'
return x
def func_B():
# Here, we are somewhat mislead. We're actually involving two different
# variables named 'x'. One is local to func_B, the other is global.
# By calling func_A(), we do two things: we're reassigning the value
# of the GLOBAL x as part of func_A, and then taking that same value
# since it's returned by func_A, and assigning it to a LOCAL variable
# named 'x'.
x = func_A() # look at this as: x_local = func_A()
# Here, we're assigning the value of 'B' to the LOCAL x.
x = 'B' # look at this as: x_local = 'B'
return x # look at this as: return x_local
Run Code Online (Sandbox Code Playgroud)
实际上,您可以func_B使用命名变量重写所有内容,x_local它可以完全相同.
该命令仅对您的函数执行更改全局x值的操作的顺序很重要.因此在我们的例子中,顺序无关紧要,因为func_B调用func_A.在此示例中,订单确实很重要:
def a():
global foo
foo = 'A'
def b():
global foo
foo = 'B'
b()
a()
print foo
# prints 'A' because a() was the last function to modify 'foo'.
Run Code Online (Sandbox Code Playgroud)
请注意,global仅需要修改全局对象.您仍然可以在函数内访问它们而无需声明global.因此,我们有:
x = 5
def access_only():
return x
# This returns whatever the global value of 'x' is
def modify():
global x
x = 'modified'
return x
# This function makes the global 'x' equal to 'modified', and then returns that value
def create_locally():
x = 'local!'
return x
# This function creates a new local variable named 'x', and sets it as 'local',
# and returns that. The global 'x' is untouched.
Run Code Online (Sandbox Code Playgroud)
注意区别之间create_locally和access_only- access_only是尽管没有调用访问全局X global,即使create_locally不使用global或者,它因为它创建了一个本地副本分配一个值.
这里的混乱是你不应该使用全局变量的原因.
kin*_*all 17
正如其他人所指出的,global当您希望该函数能够修改全局变量时,您需要在函数中声明变量.如果您只想访问它,那么您不需要global.
要详细了解一下,"修改"意味着:如果要重新绑定全局名称使其指向不同的对象,则必须global在函数中声明名称.
许多修改(mutate)对象的操作不会重新绑定全局名称以指向不同的对象,因此它们都是有效的,而不会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
Run Code Online (Sandbox Code Playgroud)
这是一种使用全局变量作为参数的默认值吸引我的情况。
globVar = None # initialize value of global variable
def func(param = globVar): # use globVar as default value for param
print 'param =', param, 'globVar =', globVar # display values
def test():
global globVar
globVar = 42 # change value of global
func()
test()
=========
output: param = None, globVar = 42
Run Code Online (Sandbox Code Playgroud)
我曾期望param的值为42。首次解析函数func时,Python 2.7评估了globVar的值。更改globVar的值不会影响分配给param的默认值。如下所示,延迟评估可以按照我的需要进行。
def func(param = eval('globVar')): # this seems to work
print 'param =', param, 'globVar =', globVar # display values
Run Code Online (Sandbox Code Playgroud)
或者,如果您想安全起见,
def func(param = None)):
if param == None:
param = globVar
print 'param =', param, 'globVar =', globVar # display values
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以直接访问函数内部的全局变量。如果要更改该全局变量的值,请使用“ global variable_name”。请参见以下示例:
var = 1
def global_var_change():
global var
var = "value changed"
global_var_change() #call the function for changes
print var
Run Code Online (Sandbox Code Playgroud)
一般来说,这不是一个好的编程习惯。通过破坏名称空间逻辑,代码可能变得难以理解和调试。