我有这个:
g_c = 0
class TestClass():
global g_c
def run(self):
for i in range(10):
g_c = 1
print g_c
t = TestClass()
t.run()
print g_c
Run Code Online (Sandbox Code Playgroud)
我怎样才能真正修改我的全局变量g_c?
unw*_*ind 85
通过global在访问它的函数内声明它:
g_c = 0
class TestClass():
def run(self):
global g_c
for i in range(10):
g_c = 1
print g_c
Run Code Online (Sandbox Code Playgroud)
在Python文档说这个,有关global声明:
全局语句是一个声明,它适用于整个当前代码块.
Mar*_*ers 12
您需要global在函数内移动声明:
class TestClass():
def run(self):
global g_c
for i in range(10):
g_c = 1
print g_c
Run Code Online (Sandbox Code Playgroud)
小智 8
我知道使用全局变量有时是最方便的事情,尤其是在使用类使最简单的事情变得如此困难的情况下(例如,multiprocessing)。我在声明全局变量时遇到了同样的问题,并通过一些实验解决了这个问题。
g_c没有被run类中的函数改变的原因是对全局名称的引用g_c不是在函数中精确建立的。Python 处理全局声明的方式实际上非常棘手。该命令global g_c有两个作用:
先决条件将键"g_c"输入到内置函数可访问的字典中,globals(). 但是,键不会出现在字典中,直到为它分配了一个值。
(可能)改变 Pythong_c在当前方法中查找变量的方式。
对(2)的充分理解是特别复杂的。首先,它只会潜在地改变,因为如果g_c在方法中没有对名称进行赋值,那么 Python 默认在globals(). 这实际上是一件相当普遍的事情,就像在代码开头一直导入的方法模块中引用的情况一样。
但是,如果赋值命令出现在方法中的任何地方,Python 默认g_c在局部变量中查找名称。即使引用发生在实际赋值之前也是如此,这将导致经典错误:
UnboundLocalError: local variable 'g_c' referenced before assignment
Run Code Online (Sandbox Code Playgroud)
现在,如果声明global g_c出现在方法内的任何地方,即使在任何引用或赋值之后,Python 默认g_c在全局变量中查找名称。但是,如果您感觉是实验性的并将声明放在引用之后,您将收到警告:
SyntaxWarning: name 'g_c' is used prior to global declaration
Run Code Online (Sandbox Code Playgroud)
如果你仔细想想,Python 中全局声明的工作方式显然与 Python 的正常工作方式交织在一起并保持一致。只是当您确实希望全局变量起作用时,规范就会变得烦人。
这是一段代码,总结了我刚才所说的内容(还有一些观察):
g_c = 0
print ("Initial value of g_c: " + str(g_c))
print("Variable defined outside of method automatically global? "
+ str("g_c" in globals()))
class TestClass():
def direct_print(self):
print("Directly printing g_c without declaration or modification: "
+ str(g_c))
#Without any local reference to the name
#Python defaults to search for the variable in globals()
#This of course happens for all the module names you import
def mod_without_dec(self):
g_c = 1
#A local assignment without declaring reference to global variable
#makes Python default to access local name
print ("After mod_without_dec, local g_c=" + str(g_c))
print ("After mod_without_dec, global g_c=" + str(globals()["g_c"]))
def mod_with_late_dec(self):
g_c = 2
#Even with a late declaration, the global variable is accessed
#However, a syntax warning will be issued
global g_c
print ("After mod_with_late_dec, local g_c=" + str(g_c))
print ("After mod_with_late_dec, global g_c=" + str(globals()["g_c"]))
def mod_without_dec_error(self):
try:
print("This is g_c" + str(g_c))
except:
print("Error occured while accessing g_c")
#If you try to access g_c without declaring it global
#but within the method you also alter it at some point
#then Python will not search for the name in globals()
#!!!!!Even if the assignment command occurs later!!!!!
g_c = 3
def sound_practice(self):
global g_c
#With correct declaration within the method
#The local name g_c becomes an alias for globals()["g_c"]
g_c = 4
print("In sound_practice, the name g_c points to: " + str(g_c))
t = TestClass()
t.direct_print()
t.mod_without_dec()
t.mod_with_late_dec()
t.mod_without_dec_error()
t.sound_practice()
Run Code Online (Sandbox Code Playgroud)
小智 5
class flag:
## Store pseudo-global variables here
keys=False
sword=True
torch=False
## test the flag class
print('______________________')
print(flag.keys)
print(flag.sword)
print (flag.torch)
## now change the variables
flag.keys=True
flag.sword= not flag.sword
flag.torch=True
print('______________________')
print(flag.keys)
print(flag.sword)
print (flag.torch)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
122780 次 |
| 最近记录: |