python 多个文件共享数据/变量
2018-02-01 18:49
405 查看
python的每个文件相当于一个module,每个module有自己的命名空间(也可以说是作用域),在多个文件中共享变量可以使用import来实现。
import有个缓存机制,即在某一次python解释器运行时,模块只导入一次、代码只执行一次,若有重复的导入操作,则会从缓存中直接读取其中的变量,举例如下:
运行run_test.py:
可见在一次python运行时,config_test.py和another_test.py共享了config.py中的变量a
参考:
http://blog.csdn.net/tuxl_c_s_d_n/article/details/45462139
https://docs.python.org/3/reference/import.html
import有个缓存机制,即在某一次python解释器运行时,模块只导入一次、代码只执行一次,若有重复的导入操作,则会从缓存中直接读取其中的变量,举例如下:
# config.py import time a = time.time()
# config_test.py import config print 'config_test:', print config.a
import config import config_test print 'another_test:', print config.a,
# run_test.py import config_test import another_test
运行run_test.py:
O:\lab\PycharmProjects\>python run_test.py config_test: 1517482042.35 another_test: 1517482042.35
可见在一次python运行时,config_test.py和another_test.py共享了config.py中的变量a
参考:
http://blog.csdn.net/tuxl_c_s_d_n/article/details/45462139
https://docs.python.org/3/reference/import.html
相关文章推荐
- Python 进程之间共享数据(全局变量)
- python unittest TestCase间共享数据(全局变量的使用)
- python在不同文件之间共享全局变量方案的探讨
- python通过配置文件共享全局变量
- python多个模块py文件的数据共享
- python中如何在不同文件间共享全局变量
- python 多模块文件共享变量
- Python 进程之间共享数据(全局变量)
- python中如何在不同文件间共享全局变量
- 用python编写脚本从wireshark导出的数据文件中提取数据
- 共享组件(有码):将文件中数据导入到DataTable
- 使用共享变量实现主报表及子报表之间的数据共享
- 进程间通过内存映射文件共享数据
- 使用水晶报表的共享变量(Shared)即可实现在主/子报表之间的数据共享
- 【转】使用共享变量实现主报表及子报表之间的数据共享
- 从一个php文件向另一个地址post数据,不用表单和隐藏的变量
- python读取文件数据绘直角坐标图
- 通过内存映射文件实现进程间数据共享
- 如何从一个php文件向另一个地址post数据,不用表单和隐藏的变量(转)
- 如何从一个php文件向另一个地址post数据,不用表单和隐藏的变量