python-自定义模块【公用】
来源:互联网 发布:多米诺a200 pc软件 编辑:程序博客网 时间:2024/05/21 09:26
python中没有公共变量,我如果要在一个文件中引用其他的变量,我需要做的将这些内容放到一个单独的py文件中,其他文件import它。值得注意的是,不管import多少次,其实都只执行一次。
1,import多少次其实只有一次
比如,我要读一个字典到内存,以后执行的若干文件都会用到这个公共字典:
其他文件:
from module_ownerMap import owner_to_project_dict
module_ownerMap.py
# -* - coding: UTF-8 -* -#将owner_to_project这个表读到内存中,以便后续比较旧的owner名称与新的owner名称def read_owner_to_project(): c = open("owner_to_project").read() list = c.split("\n") dict = {} for i in list: if i != "": list2 = i.split(",") dict[list2[0]] = list2[2] return dictowner_to_project_dict = read_owner_to_project()
2,使用一个公用静态的变量
静态变量文件static.py
list = []模块1文件module1.py
from static import listlist.append("1")模块2文件module2.py
from static import listlist.append("2")主文件main.py
from static import listimport module1import module2print list主文件运行结果:
administrator@ubuntu:~/test/downloadBBS$ python main.py['1', '2']
3,不能在使用os.system中用一个静态的,因为这其实不在一个环境下了
静态变量文件static.py
list = []
另一个main文件 main2.py
from static import listlist.append("2")主文件main.py
from static import listimport os,sysos.system("python main2.py")print list主文件结果:
administrator@ubuntu:~/test/downloadBBS$ python main.py[]
- python-自定义模块【公用】
- Python添加自定义模块
- 【python】导入自定义模块
- python 自定义模块
- python引入自定义模块
- python导入自定义模块
- python引入自定义模块
- python导入自定义模块
- Python导入自定义模块
- Python 自定义模块导入
- python导入自定义模块
- python导入自定义模块
- python自定义模块
- python调用自定义模块
- Python导入自定义模块
- Python 之自定义模块
- python import 自定义 模块
- python自定义模块
- 华为一面的心得
- xcode4.3.2 代码自动完成竟然出问题
- const、static、extern、volatile的用法详解
- 模版特化
- linux一些系统调用的再实现
- python-自定义模块【公用】
- MMSeg中文分词算法
- OALL8 处于不一致状态
- jqGrid不支持IE8的解决办法(Grid can not be used in this ('quirks') mode)
- [译] Threads, Events and QObjects
- [Script]Check status of all the Concurrent Managers
- 详解AJAX核心 —— XMLHttpRequest 对象
- 为什么很多公司都是用PADS和orCAD的组合来设计硬件呢?
- Kinect体感机器人(三)—— 空间向量法计算关节角度