主要目的是将重复定义的模块独立出来,便于其他脚本直接引用,减少反复定义的问题 module1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import os,sys import inspect if __name__ == '__main__': pass else: # python3 if sys.version>'3': frm_list = inspect.stack() for item in frm_list: if item.index is None: # print(item) continue else: if __file__ in item.filename: # print(item) continue else: # print(item) frm = item mod = inspect.getmodule(frm[0]) t_file = str(os.path.abspath(mod.__file__)) # python2 else: frm_list = inspect.stack() mod = inspect.getmodule(frm_list[1][0]) t_file = str(os.path.abspath(mod.__file__)) t_list = [1,2,3] t_dict = {"k1":"v1"} |
module2:
1 2 3 4 5 6 7 8 9 10 11 |
import time from module1 import * # print(t_name) print(id(t_file),t_file) print(id(t_list),t_list) print(id(t_dict),t_dict) time.sleep(10) |
分别运行module2,返回结果如下:
1 2 3 4 5 6 7 8 9 10 11 |
# python2 module2.py (139742777226544, '/home/software/boofuzz/ft_local/module2.py') (139742781893304, [1, 2, 3]) (139742781927784, {'k1': 'v1'}) python3 module2.py 139720418778448 /home/software/boofuzz/ft_local/module2.py 139720418165064 [1, 2, 3] 139720421308600 {'k1': 'v1'} |
参考: https: […]