
上QQ阅读APP看书,第一时间看更新
4.3.5 全局字典——sys.modules模块
sys.modules是一个全局字典,这个字典是Python启动后就加载在内存中的。每当程序员导入新的模块,sys.modules都将记录这些模块。字典sys.modules对加载模块起到了缓存的作用。当某个模块第一次导入时,字典sys.modules将自动记录该模块。当第2次再导入此模块时,Python会直接到字典中查找,从而加快了程序运行的速度。
字典sys.modules具有字典所拥有的一切方法,可以通过这些方法了解当前的环境加载了哪些模块。下面这段代码调用了字典的keys()和values()方法,keys()返回当前环境下加载的模块,values()返回这些模块引用路径。
01 import sys # 导入sys模块 02 print (sys.modules.keys()) 03 print (sys.modules.values()) 04 print (sys.modules["os"])
【代码说明】
·第2行代码keys()返回sys模块以及Python自动加载的模块。输出结果:
dict_keys(['sys', 'builtins', '_frozen_importlib', '_imp', '_thread', '_warnings', '_weakref', 'zipimport', '_frozen_importlib_external', '_io', 'marshal', 'nt', 'winreg', 'encodings', 'codecs', '_codecs', 'encodings.aliases', 'encodings.utf_8', '_signal', '__main__', 'encodings.latin_1', 'io', 'abc', '_abc', 'site', 'os', 'stat', '_stat', 'ntpath', 'genericpath', 'os.path', '_collections_abc', '_sitebuiltins', 'idlelib', 'idlelib.run', 'linecache', 'functools', '_functools', 'collections', 'operator', '_operator', 'keyword', 'heapq', '_heapq', 'itertools', 'reprlib', '_collections', 'tokenize', 're', 'enum', 'types', 'sre_compile', '_sre', 'sre_parse', 'sre_constants', '_locale', 'copyreg', 'token', 'queue', 'threading', 'time', 'traceback', '_weakrefset', '_queue', 'warnings', 'idlelib.autocomplete', 'string', '_string', 'idlelib.autocomplete_w', 'platform', 'subprocess', 'signal', 'errno', 'msvcrt', '_winapi', 'tkinter', '_tkinter', 'tkinter.constants', 'idlelib.multicall', 'idlelib.config', 'configparser', 'collections.abc', '_bootlocale', 'encodings.gbk', '_codecs_cn', '_multibytecodec', 'idlelib.hyperparser', 'idlelib.pyparse', 'idlelib.calltip', 'inspect', 'dis', 'opcode', '_opcode', 'importlib', 'importlib._bootstrap', 'importlib._bootstrap_external', 'importlib.machinery', 'textwrap', 'idlelib.calltip_w', 'idlelib.tooltip', 'idlelib.debugger_r', 'idlelib.debugger', 'bdb', 'fnmatch', 'posixpath', 'idlelib.macosx', 'plistlib', 'binascii', 'contextlib', 'datetime', 'math', '_datetime', 'struct', '_struct', 'xml', 'xml.parsers', 'xml.parsers.expat', 'pyexpat.errors', 'pyexpat.model', 'pyexpat', 'xml.parsers.expat.model', 'xml.parsers.expat.errors', 'idlelib.scrolledlist', 'idlelib.window', 'idlelib.debugobj_r', 'idlelib.rpc', 'pickle', '_compat_pickle', '_pickle', 'select', 'socket', '_socket', 'selectors', 'socketserver', 'idlelib.iomenu', 'shlex', 'tempfile', 'shutil', 'zlib', 'bz2', '_compression', '_bz2', 'lzma', '_lzma', 'random', 'hashlib', '_hashlib', '_blake2', '_sha3', 'bisect', '_bisect', '_random', 'weakref', 'locale', 'idlelib.stackviewer', 'idlelib.debugobj', 'idlelib.tree', 'idlelib.zoomheight', 'pydoc', 'importlib.util', 'importlib.abc', 'pkgutil', 'urllib', 'urllib.parse', 'ast', '_ast', 'copy'])
·第3行代码values()返回的模块引用。
·第4行代码返回索引“os”对应的引用。输出结果:
<module 'os' from 'C:\\Users\\hchlcomputer\\AppData\\Local\\Programs\\Python\\Python37\\ lib\\os.py'>
下面这段代码实现了导入模块的过滤。
01 import sys # 导入sys模块 02 d = sys.modules.copy() 03 import copy,string 04 for 2 in zip(set(sys.modules) - set(d)): # 使用zip进行modules的过滤 05 Print(2)
【代码说明】
·第2行代码调用copy(),把当前导入的模块信息保存到字典d中。
·第3行代码导入模块copy、string,此时的字典sys.modules包含了原有的模块和新导入的模块。
·第4行代码调用set()把字典sys.modules、字典d存入set集合中,set集合实现了减法运算符,set(sys.modules)-set(d)将返回字典d中没有、而字典sys.modules中存在的模块。然后调用zip()对set集合“解包”,返回一个支持遍历的对象。遍历后输出结果:
('copy',)