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

eshow 2007-07-27 02:29:00
一个简单的实验:

假设现在有两个模块,模块varmain.py负责声明并初始化全局变量,同时作为main的入口调用其他辅助模块完成程序功能;模块varprinter负责打印全局变量,你可以想像该模块是需要访问全局变量并使用它们完成某些具体的业务工作。

代码如下:

#!/usr/bin/env python
#module varmain.py

import varprinter
name = ''

def loadName():
global name
name = 'Hello!'


if __name__ == '__main__':
loadName()
print 'in main name = ', name
varprinter.printName()



#!/usr/bin/env python
#module varprinter.py

from varmain import name

def printName():
print 'in varprinter name = ', name


运行后的结果为:

in main name = Hello!
in varprinter name =

即全局变量name并没有保持。

一些简单的修改如在varprinter.py中把import改成from import,在varmain.py中撤掉loadName()函数,而将name变量的修改直接放到if __name__ == '__main__':下等招数都试过了,效果一样。

各位请调试成功后回帖,另外也不要修改程序的整体结构,在前面已经说清楚了,varmain模块是负责管理全局变量的,可以想像是从数据库或者文件等持久层读入全局变量;varprinter是访问和使用全局变量来完成业务功能的。不知各位高手是否有可行性的建议?谢谢!
...全文
2106 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
iambic 2007-09-05
  • 打赏
  • 举报
回复
os.environ可以用来共享数据,但是只能用字符串类型。
hunter_gio 2007-09-05
  • 打赏
  • 举报
回复
楼主用过os.environ么?

environ
A mapping object representing the string environment. For example, environ['HOME'] is the pathname of your home directory (on some platforms), and is equivalent to getenv("HOME") in C. This mapping is captured the first time the os module is imported, typically during Python startup as part of processing site.py. Changes to the environment made after this time are not reflected in os.environ, except for changes made by modifying os.environ directly. If the platform supports the putenv() function, this mapping may be used to modify the environment as well as query the environment. putenv() will be called automatically when the mapping is modified. Note: Calling putenv() directly does not change os.environ, so it's better to modify os.environ. Note: On some platforms, including FreeBSD and Mac OS X, setting environ may cause memory leaks. Refer to the system documentation for putenv(). If putenv() is not provided, a modified copy of this mapping may be passed to the appropriate process-creation functions to cause child processes to use a modified environment. If the platform supports the unsetenv() function, you can delete items in this mapping to unset environment variables. unsetenv() will be called automatically when an item is deleted from os.environ.
Jahson 2007-08-31
  • 打赏
  • 举报
回复
这问题比较有意思,等待答案中
zwo0o 2007-07-31
  • 打赏
  • 举报
回复
你如能把varprinter.py改为:
#!/usr/bin/env python
#module varprinter.py

from varmain import name

def printName(name):
print 'in varprinter name = ', name
就可以打印出来!
iambic 2007-07-27
  • 打赏
  • 举报
回复
varmain.py实际上是两个模块,一个是__main__,一个是varmain,也就是说,这个文件会被载入两次。
你设置的是__main__中的hello,不是varmain中的hello。
xyzxyz1111 2007-07-27
  • 打赏
  • 举报
回复
把文件varprinter.py中的
print 'in varprinter name = ', name
换成
import sys
print 'in varprinter name = ', sys.modules['__main__'].name

也不需要from varmain import name这行了

37,718

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧