《Python基础教程》复读笔记(4)-模块
Reading Time:The full text has 284 words, estimated reading time: 2 minutes
Creation Date:2017-04-12
Previous Article:PostgreSQL函数(存储过程)--案例分析
Next Article:《Python基础教程》复读笔记(3)
BEGIN
引入模块
以pprint模块的pprint方法为例
- 直接引入模块
引入模块:
import pprint调用方法:pprint.pprint() - 引入模块方法(可直接使用)
引入pprint方法:
from pprint import pprint引入pprint模块的所有方法:from pprint import *调用方式:pprint() - 给模块设置别名
import pprint as pp
pp.pprint() == pprint.pprint()
or
from pprint import pprint as pp
pp() == pprint()
加入环境变量
将模块加入Python环境变量中方便直接调用
import sys
sys.path.append('/path/to/xxx')
重要属性
pprint.__name__:显示模块名。若单独执行则__name__ == '__main__',可以作为程序自动启动的入口。- 众多模块放在一个文件夹下可以作为包存在,在目录下新建文件
__init__说明此目录为一个包。 dir(pprint)可列出模块的所有方法,其中固有的有:'__builtins__', '__doc__', '__file__', '__name__', '__package__'- 可通过设置
__all__的值,告诉解释器模块向外提供的方法,此时如果from pprint import *方式引入模块方法,则引入__all__定义的方法,若__all__未定义则引入全部。 - 查看模块路径:
pprint.__file__ - 查看模块文档:
pprint.__doc__或help(pprint)
FINISH
Previous Article:PostgreSQL函数(存储过程)--案例分析
Next Article:《Python基础教程》复读笔记(3)