且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在导入的模块之间共享全局变量?

更新时间:2023-11-14 08:16:16

如果需要的话,创建一个具有共享全局变量的设置模块.这样,您只需一次导入和引用每个全局变量,并将它们隔离在settings模块的命名空间中.这是一件好事.

Create a settings module that has shared globals if that's what you want. That way you're only importing and referencing each global one time, and you're keeping them isolated within the namespace of the settings module. It's a good thing.

#settings.py
var = 'this is global'

# a.py
import settings
import b
b.foo()

# b.py
import settings
def foo():
    print settings.var