不同模块之间的全局变量 [英] Global Variables between different modules

查看:213
本文介绍了不同模块之间的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

main.py:

from module import *   
var=10

def func2():
    print "func2:" ,var

def main():
    global var
    var=20
    print "main - before", var
    func2()
    func1()
    print "main - after", var

if __name__=='__main__':
    main()

module.py

from main import *

def func1():
    global var
    print "func1:", var

程序打印:

主要-之前:20

main - before: 20

func2:20

func1:10

主要-20点后

'var'是全局变量.我希望当我更改var的值时,无论变量'var'出现在哪里,它都会被更改. func1和func2之间的唯一区别是func1在另一个模块中.仍然,我不明白为什么func1和func2之间的'var'值不同.

'var' is a global variable. I would expect that the moment I change var's value, it will be changed wherever the variable 'var' appears. The only difference between func1 and func2 is that func1 is in another module. Still, I don't understand why the value of 'var' is different between func1 and func2.

推荐答案

在python中没有真正的全局变量.对象绑定到命名空间中的变量,并且global关键字引用当前的 module 命名空间. from somemodule import *在当前模块的命名空间中创建新变量,并将其引用到 somemodule 的对象.现在,您有两个不同的变量指向同一个对象.如果您重新绑定其中一个变量,则其他变量将继续引用原始对象.此外,function.s的全局"命名空间是在其中定义的模块,即使将其导入到其他模块也是如此.

There is no such thing as a truly global variable in python. Objects are bound to variables in namespaces and the global keyword refers to the current module namespace. from somemodule import * creates new variables in the current module's namespace and refers them to somemodule's objects. You now have two different variables pointing to the same object. If you rebind one of the variables, the other ones continue to reference the original object. Further, a function.s "global" namespace is the module it is defined in, even if it is imported to a different module.

如果需要全局"变量,请导入模块并使用其名称空间限定名称,而不是在本地名称空间中重新绑定各个变量.

If you want a "global" variable, import the module and use its namespace qualified name instead of rebinding individual variables in the local namespace.

这是一个带注释的示例...

Here's an annotated example...

cfg.py

var = 10
somelist = []
var2 = 1

def show_var2():
    print var2

main.py

import cfg
from cfg import *   # bind objects referenced by variables in cfg to
                    # like-named variables in this module. These objects
                    # from 'cfg' now have an additional reference here

if __name__ == '__main__':
    print cfg.var, var
    var = 20        # rebind this module's 'var' to some other value.
                    # this does not affect cfg's 'var'
    print cfg.var, var

    print '----'
    print cfg.somelist, somelist
    somelist.append(1)    # update this module's 'somelist'
                          # since we updated the existing object, all see it
    print cfg.somelist, somelist

    print '----'
    var2 = 2
    print var2, cfg.var2, show_var2() # show_var2 is in cfg.py and uses its
                                      # namespace even if its been imported
                                      # elsewhere

这篇关于不同模块之间的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆