python 中global() 函数

https://www.cnblogs.com/dflblog/p/11364131.html

两个函数中调用变量

def test1():
    a = 3
    return a


def test2():
    globals()['b'] = 'tarzan'   #globals()函数返回字典当前所有的全局变量,并且可以往字典里面添加数据。
    print(globals())
def test3():
    #想要调用test1中a
    a = test1()
    #不要想调用函数。定一个全局变量也可以。但这次试用globle()函数 调用test2中的b值
    print(globals())   #他和2中的一样,  这里有个坑、首先你要先运行test2,才能正常运行test3.自己尝试一下
    name = globals()['b']
    print(name)


if __name__ == '__main__':
    test2()
    test3()
原文地址:https://www.cnblogs.com/tarzen213/p/12201152.html