Python 基础教程】 第10章 开箱即用 - 标准库和模块 【暂停】


10.1 模块

>>> import math # math:模块
>>> math.sin(0) # sin:函数
0

10.1.1 模块就是程序

# 导入模块,告诉解释器去哪里查找即将导入的模块

>>> import hello!
#报错,程序运行的环境里没有这个模块(.py文件),因此需要给定路径后再导入

>>> import sys
>>> sys.path.append('C:/python')
>>> import hello
Hello world!
# Unix: sys.path.append('/home/Admir/python')

10.1.2 模块是用来下定义的

1、要让代码可重复调用的,必须将其模块化
2、编写测试代码来检测模块是否可用

# hello.py
def hello():
    print('Hello world!')
def test():
    hello()

# 同时用于在模块内部执行函数
if __name__ == '__main__':
    test()  


----------
>>> import hello
>>> hello.test()
Hello world!

10.1.3 告诉解释器查找模块

# 告诉了解释器去哪里找模块
# pprint 模块中的 pprint 函数可以将结果打印**成**多行数据
import sys,pprint
pprint.pprint(sys.path)


----------
['',
 '/Applications/anaconda3/lib/python36.zip',
 '/Applications/anaconda3/lib/python3.6',
 '/Applications/anaconda3/lib/python3.6/lib-dynload',
 '/Applications/anaconda3/lib/python3.6/site-packages',
 '/Applications/anaconda3/lib/python3.6/site-packages/aeosa',
 '/Applications/anaconda3/lib/python3.6/site-packages/IPython/extensions',
 '/Users/haozhiqiang/.ipython']

前面将模块位置加入到 path 的做法不常见,标准做法是将模块所在的目录包含在环境变量 PYTHONPATH 中。


10.2 探索模块


10.2.1 模块包括什么

import copy

1、使用 dir

# 查明模块包含那列东西,可使用函数 dir , 它将列出对象的所有属性
>>> dir(copy)
['Error',
 '__all__',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_copy_dispatch',
 '_copy_immutable',
 '_deepcopy_atomic',
 '_deepcopy_dict',
 '_deepcopy_dispatch',
 '_deepcopy_list',
 '_deepcopy_method',
 '_deepcopy_tuple',
 '_keep_alive',
 '_reconstruct',
 'copy',
 'deepcopy',
 'dispatch_table',
 'error']

# 有下划线的是我们无法调用的,因此可以用列表推导的方法将其过滤掉
>>> [n for n in dir(copy) if not n.startwith('_')]
['Error', 'copy', 'deepcopy', 'dispatch_table', 'error']

2、变量 all
all 在模块 copy 中是这样设置的:

__all__ = ["Error","copy","deepcopy"]

作用:模块可能包含大量其他程序不需要的变量、函数和类,设置all可以把它们全部过滤掉。

# 在 from copy import * 时,只会导入 __all__ 下的函数 
>>> copy.__all__
['Error', 'copy', 'deepcopy']

10.2.2 使用 help 获取帮助

>>> help(copy.copy)

Help on function copy in module copy:

copy(x)
    Shallow copy operation on arbitrary Python objects.

    See the module's __doc__ string for more info.

#  See the module's __doc__ string for more info.
>>> copy.copy__doc__ # 查看文档字符串
"Shallow copy operation on arbitrary Python objects.\n\n    See the module's __doc__ string for more info.\n    "

10.2.3 文档

这里,doc是查看文档字符串,可以看到开发者对于该函数的描述,如下

# 查看文档字符串
def test():
    'This the test\'s doc!' # 文档字符串
    return

>>> test.__doc__
"This the test's doc!"

10.2.4 使用源代码

阅读源代码是理解Python的最佳方式。可以通过如下方式查看到源代码的路径。

# 源代码的一些模块可能是C语言编写的
>>> print(copy.__file__)
/Applications/anaconda3/lib/python3.6/copy.py

10.3 标准库:一些深受欢迎的模块


10.3.1 sys

模块 sys 能够访问与 Python 解释器紧密相关的变量和函数

10.3.2 os

模块 os 让你能够访问多个操作系统服务。


10.3.3 fileinput

模块 fileinput 让你能够轻松的迭代一系列文本文件中的所有行


10.3.4 集合、堆和双端队列

1、集合
集合由内置类 set 实现

>>> set(range(5))
{0,1,2,3,4}
# 集合主要用于成员资格检查,因此将忽略重复的元素
>>> set([1,2,2])
{1,2}

>>> set((1,2,2))
{1,2}
# 计算两个集合的并集,调用方法 union
>>> a = {1,2,3}
>>> b = {3,4,5}
>>> a.union(b)
{1, 2, 3, 4, 5}

# 按位或运算
>>> a|b
{1, 2, 3, 4, 5}
>>> a = {1,2,3}
>>> b = {3,4,5}

>>> a & b
{3}
>>> a.intersection(b)
{3}

>>> c = a & b
>>> c.issubset(a) # c 包含于 a
True
>>> c <= a
True

>>> c.issuperset(a) # c supper a 
False
>>> c >= a
False

>>> a.intersection(b)
{3}
>>> a&b
{3}

>>> a.diffent(b)
{1,2}
>>> a - b
{1,2}

>>> a.symmetric_difference(b) # a 不同于 b
{1, 2, 4, 5}
>>> a ^ b
{1, 2, 4, 5}

>>> a.copy()
{1,2,3}
>>> a.copy() is a
False
>>> a.copy() == a
True
myset = []
for i in range(10):
    myset.append(set(range(i,i+5)))

from functools import reduce
reduce(set.union,myset)


----------
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
a = {1,2,3}
b = {3,4,5}
a.add(b) is wrong

the right way is :
a.add(forzenset(b))

2、堆

猜你喜欢

转载自blog.csdn.net/weixin_37392582/article/details/80430352
今日推荐