sys.argv详见python文件操作笔记
1.sys.version
获取Python解释程序的版本信息
import sys
pythonVersion = sys.version
print(pythonVersion)
'''
3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)]
'''
2.sys.exit(n)
退出程序,正常退出时exit(0)
a = 2
if a == 1:
sys.exit(0)
else:
sys.exit(2)
'''
Process finished with exit code 2
'''
3.sys.platform
返回操作系统平台名称
platformName = sys.platform
print(platformName)
'''
darwin
'''
为什么在Mac OS X上键入sys.platform时,输出是“darwin”?
答:因为Mac OS X的核心是 the Darwin OS
从链接的WikiPedia页面引用:
Darwin forms the core set of components upon which Mac OS X and iOS are based.
Python仅使用相同的平台标识符。
4.sys.modules
Python中所有加载到内存的模块都放在sys.modules。
sys.modules是一个全局字典,字典sys.modules对于加载模块起到了缓冲的作用,当import一个模块时首先会在这个列表中查找是否已经加载了此模块,如果加载了则只是将模块的名字加入到正在调用import的模块的Local名字空间中。当第二次再导入该模块时,python会直接到字典中查找,从而加快了程序运行的速度。
模块文件可以是py、pyc、pyd,找到后将模块载入内存,并加入到sys.modules中,并将名称导入到当前的Local名字空间。
sys.modules[name]
A way to get a handle to the current running module in Python:
import sys
module = sys.modules[name]
it really only works if you are doing the whole sys.modules litany in the very module you want to get a handle to.
所以,getattr(sys.modules[**__name**__], func_name)的含义便是找到当前文件下名称为func_name的对象(类对象或者函数对象)。