def definition():
global storage_account_connection_string
storage_account_connection_string="test"
def load_config():
config = ConfigParser.ConfigParser()
config.readfp(open(r'config.txt'))
temp = config.get('API_Metrics','SCC')
temp1 = temp.split("#")
for conf in temp1:
confTemp=conf.split(":")
print "#########################################"
print confTemp[0]
print confTemp[1]
print confTemp[2]
storage_account_connection_string=confTemp[2]
print storage_account_connection_string
get_details()
def get_details():
print storage_account_connection_string
print "Blob",blob_name_filter
if __name__ == '__main_`enter code here`_':
definition()
load_config()`enter code here`
我的问题是,为什么连接字符串总是在get_details()中显示“ test”,尽管在load_config()中为其分配了一些值,我还是丢失了一些东西?
答案 0 :(得分:1)
我无法为您运行代码,但是如果我明白了这一点,这应该有助于调试:
def definition():
global storage_account_connection_string
storage_account_connection_string="test"
def load_config():
global storage_account_connection_string
storage_account_connection_string = "Whathever"
def get_details():
print storage_account_connection_string
definition()
get_details()
load_config()
get_details()
或者这个:
storage_account_connection_string="test" # <-- outside in "global scope"
def load_config():
global storage_account_connection_string
storage_account_connection_string = "Whathever"
def get_details():
print storage_account_connection_string
get_details()
load_config()
get_details()
答案 1 :(得分:1)
检查此示例:
def a():
global g
g = 2 # -> this is global variable
def b():
g = 3 # -> this is function variable
print(g)
def c():
print(g) # -> it will use global variable
a()
b() # 3
c() # 2
在您的情况下,您需要将此函数添加全局
....
def load_config():
global storage_account_connection_string
....