在python中访问全局变量值

在python中访问全局变量值,python,global-variables,Python,Global Variables,我的问题是为什么连接字符串总是在get_details中打印测试,尽管它在load_config中被分配了一些值,但我是否遗漏了什么 我无法运行您的代码,但如果我明白了这一点,这将有助于调试: def definition(): global storage_account_connection_string storage_account_connection_string="test" def load_config(): config = ConfigParser

我的问题是为什么连接字符串总是在get_details中打印测试,尽管它在load_config中被分配了一些值,但我是否遗漏了什么

我无法运行您的代码,但如果我明白了这一点,这将有助于调试:

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`
或者这个:

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()
在您的情况下,您需要将全局添加到此函数

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

尝试减少您的代码并制作一个简单、完整且可验证的示例:。现在我无法运行您的代码,因为我没有config.txt文件,而且一些打印是冗余的,与问题无关,解析配置可能与问题也无关。制作一个尽可能小的示例,代码示例可以重现您的问题,并且可以在不进行修改的情况下运行。
....
def load_config():
    global storage_account_connection_string
    ....