Python基础(协程函数、内置函数、递归、模块和包)-day05

时间:2021-11-26 19:54:39

写在前面



    凭着爱,再回首;



一、协程函数(生成器:yield的表达式形式)

  1.yield 的语句形式: yield 1

    - 这种方式在 Python基础(函数部分)-day04 里面详细介绍过,这里不再赘述;

  2.yield 的表达式形式: x=yield

    - 示例1:使用 next() 方法调用生成器执行,没有给yield传值;

 def deco(func):                              # 定义了一个装饰器
def wrapper(*args,**kwargs):
res = func(*args,**kwargs)
next(res) # 就是为了先执行一下 next方法
return res
return wrapper @deco
def eater(name):
print('%s ready to eat' % name)
while True:
food=yield
print("%s start to eat %s" % (name, food))
g = eater('alex') # 得到一个生成器函数
next(g) # 第一次调用 生成器的next方法;(装饰器里提前执行了一次 next方法)
next(g) # 第二次调用... ---
alex ready to eat # 装饰器里调用 next() 的打印结果
alex start to eat None # 第一次调用 next() 的打印结果
alex start to eat None # 第二次调用...

    - 示例2:使用 send() 方法调用生成器执行,并给yield传值;

 def deco(func):
def wrapper(*args,**kwargs):
res = func(*args,**kwargs)
next(res)
return res
return wrapper @deco
def eater(name):
print('%s ready to eat' % name)
food_list = []
while True:
food=yield
food_list.append(food)
print("%s start to eat %s" % (name, food))
print("%s have eaten: %s" % (name,food_list))
g = eater('alex')
g.send('tomato')                    # 第一次调用生成器执行,传入 'tomato' 给 yield,然后由yield赋值给food变量;然后向下执行进入下一次while循环,暂停并等待;
g.send('potato')
g.send('beef')
g.send('rice') ---
alex ready to eat
alex start to eat tomato
alex have eaten: ['tomato']
alex start to eat potato
alex have eaten: ['tomato', 'potato']
alex start to eat beef
alex have eaten: ['tomato', 'potato', 'beef']
alex start to eat rice
alex have eaten: ['tomato', 'potato', 'beef', 'rice']

    注意:next(g) #等同于 g.send(None)

  3.yield表达式形式的应用

 #!/usr/bin/python
# -*- coding:utf-8 -*- # 实现Linux下的如下命令: grep -rl 'python' path
# 即给出一个目录和patten,递归遍历查找该目录下含有该patten的文件完整路径,如果文件含有多行则需要去重; import os def init(func): # 定义一个执行 next() 功能的装饰器;
def wrapper(*args,**kwargs):
res = func(*args,**kwargs)
next(res)
return res
return wrapper
@init
def search_all_file(target):
while True:
path = yield # 利用send()给yield传值,然后赋值给path
g = os.walk(path) # 根据给定path,遍历该目录下所有文件,也包含所有子目录下的文件;
for path,_,files in g:
for file in files:
target.send(r'%s\%s' % (path,file)) # 利用send()把遍历得到的所有文件传递给下一个生成器的yield;不接受yield的返回值;
@init
def opener(target):
while True:
file_to_open = yield # 接收调用者传递的文件名,即 search_all_file()函数里send()的参数;
with open(file_to_open,encoding='utf-8') as rf:
for line in rf: # 打开文件,遍历该文件内容;
res = target.send((file_to_open,line)) # 利用send()把该文件名和每次遍历的每行内容传递给下一个生成器的yield;并用res接收返回值;
if res: # 需要注意的一点:send()参数只能是一个值,多个参数则可以以元组的形式传递;
break
@init
def grep(patten):
flag = False # 定义一个标识位
while True:
the_file,line = yield flag # 接收调用者传递的文件名和该文件名下遍历得到的每行内容;
flag = False
if patten in line:
flag = True # 如果某个文件某行内容包含 patten 关键字,则将标识位置为 True,并再下一次调用的时候返回给调用者;
print(the_file) # 打印匹配到的文件名; path = r'D:\soft\work\Python_17\day05\a' # path=r'path' --> r'xxx' 表示绝对 row data,表示不用转意;
g = search_all_file(opener(grep('python'))) # 注意这里的调用写法!!!
print(g)
g.send(path)

二、递归调用

  1.递归的概念

    在函数调用过程中,直接或间接地调用了函数本身,这就是函数的递归调用;

    包括两部分:递推和回溯;

 def f1():
print('from f1')
f1() f1()

  2.Python3中递归的层数限制

 import sys
print(sys.getrecursionlimit())    # 查看默认递归次数上限
sys.setrecursionlimit(2000)      # 认为设置递归上限
print(sys.getrecursionlimit()) ---
1000
2000

  3.递归特性

    1. 必须有一个明确的结束条件;

    2. 每次进入更深一层递归时,问题规模相比上次递归都应有所减少;

    3. 递归效率不高,递归层次过多会导致栈溢出(在计算机中,函数调用是通过栈(stack)这种数据结构实现的,每当进入一个函数调用,栈就会加一层栈帧;每当函数返回,栈就会减一层栈帧。由于栈的大小不是无限的,所以,递归调用的次数过多,会导致栈溢出);

    4.堆栈扫盲:http://www.cnblogs.com/lln7777/archive/2012/03/14/2396164.html

    5.尾递归优化:http://egon09.blog.51cto.com/9161406/1842475

  4.递归具体应用

    1.二分法查找某个元素

      前提:输入的可循环对象必须是有序的;

 num_list = [1, 3, 6, 7, 9, 12, 14, 16, 17, 18, 20, 21, 22, 23, 30, 32, 33, 35,99]

 def find_num(num_list, number):
if 0 == len(num_list):
print('No exist.')
return
mid_index = len(num_list)//2
mid_value = num_list[mid_index]
if number > mid_value:
num_list = num_list[mid_index+1:]
find_num(num_list,number)
elif number < mid_value:
num_list = num_list[:mid_index]
find_num(num_list,number)
else:
print('Find it.') find_num(num_list,2)

    2.DNS 的递归和迭代查找

      参考:DNS递归查询和迭代查询的区别

    3.Linux深水炸弹

      参考:经典的Fork炸弹

三、内置函数

  1.匿名函数lambda

    - 匿名函数就是不需要显式的指定函数名,定义完就失效了;

 f=lambda x,y:x+y                   # lambda作为一个表达式,定义了一个匿名函数,功能就是返回两个参数的和;
print(f)
print(f(1,2)) print('----------')
def f2(x,y): # 函数实现
return x+y
print(f2)
print(f2(1,2)) ---
<function <lambda> at 0x00000000006CC400>
3
----------
<function f2 at 0x00000000006CC488>
3

    - lambda表达式定义的匿名函数,通常用来和其他函数搭配使用;

    - 示例如下:找出字典中value值对应的key;

 # 都是整数的列表可用max取得最大值
l=[3,213,1111,31121]
print(max(l)) # 字典怎么取值呢?
dic={'k7':10,'k2':100,'k3':30}
print(max(dic)) # 在对字典进行数据操作的时候,默认只会处理key,而不是value;所以在 'k7' 'k2' 'k3' 里选出最大的一个:'k7'
print(dic[max(dic,key=lambda k:dic[k])]) # key=lambda k:dic[k] --> 表示更改字典默认处理 value; ---
31121
k7
100

    - 更多使用见后面例子;

  2.max 和 min

    - 返回最大值/最小值;

 def max(*args, key=None): # known special case of max
"""
max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value With a single iterable argument, return its biggest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the largest argument.
"""

    - 示例1:max + lambda 实现找出字典中最大value值对应的key

1 dic={'k7':10,'k2':100,'k3':30}
2 max_key = max(dic,key=lambda k:dic[k])
3 print(max_key,dic[max_key])
4
5 ---
6 k2 100

  3.sorted

    - 返回一个按从小到大排序的列表;

 dic={'k7':10,'k2':100,'k3':30}
print(sorted(dic))
res = zip(dic.values(),dic.keys())
print(sorted(res)) ---
['k2', 'k3', 'k7']
[(10, 'k7'), (30, 'k3'), (100, 'k2')]

    - 升序和降序;

 dic={'k7':10,'k2':100,'k3':30}
print(sorted(dic)) # 默认是按照字典的key值进行排序
print(sorted(dic,key=lambda k:dic[k])) # 将字典的key和value翻转,按照value大小进行排序,按照value值的从小到大
print(sorted(dic,key=lambda k:dic[k],reverse=True)) # 按照value值的从大到小排序 ---
['k2', 'k3', 'k7']
['k7', 'k3', 'k2']
['k2', 'k3', 'k7']

  4.zip

    - zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个tuple列表;

    - 使用zip把字典的keys和values翻转过来;

 dic={'k7':10,'k2':100,'k3':30}
print(type(dic))
for item in dic.items():
print(item)
res_dic = zip(dic.values(),dic.keys())
print(type(res_dic))
# print(next(res_dic))
for item in res_dic:
print(item) ---
<class 'dict'>
('k7', 10)
('k2', 100)
('k3', 30)
<class 'zip'>
(10, 'k7')
(100, 'k2')
(30, 'k3')

    - 示例2:翻转字典的key和value,并且找出最大的value值;

 dic={'k7':10,'k2':100,'k3':30}

 print(max(dic))
res = zip(dic.values(),dic.keys())
print(type(res),res)
print(max(res)) ---
k7
<class 'zip'> <zip object at 0x00000000006C6BC8>
(100, 'k2')

    - 示例3:zip 补充

 dic={'k7':10,'k2':100,'k3':30}

 res = zip(dic.values(),dic.keys())
print(type(res),res)
print(res.__next__()) # 具有 __next__() 方法,所以是一个迭代器;
print(res.__next__())
print(res.__next__())
# print(res.__next__()) # 抛出 StopIteration异常 ---
<class 'zip'> <zip object at 0x0000000000775C08>
(10, 'k7')
(30, 'k3')
(100, 'k2')

    - 示例4:

 s='helloo'
l=[1,2,3,4,5,6,7,8]
m=('a','b','c','d')
z=zip(s,l,m)
print(z)
for i in z:
print(i) ---
<zip object at 0x0000000000B26D48>
('h', 1, 'a')
('e', 2, 'b')
('l', 3, 'c')
('l', 4, 'd')

  5.map

    - 接收一个函数 f 和一个list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回;

     """
map(func, *iterables) --> map object Make an iterator that computes the function using arguments from
each of the iterables. Stops when the shortest iterable is exhausted.
"""

    - 示例:

 l=['alex','wupeiqi','yuanhao']
res=map(lambda x:x+'_SB',l)
print(res)
print(list(res))
nums=(2,4,9,10)
res1=map(lambda x:x**2,nums)
print(list(res1)) ---
<map object at 0x00000000007E3400>
['alex_SB', 'wupeiqi_SB', 'yuanhao_SB']
[4, 16, 81, 100]

  6.reduce

    - reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接收两个参数,reduce()对list的每个元素反复调用函数f,并返回最终结果值;

     """
reduce(function, sequence[, initial]) -> value Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.
"""

    - 示例1:

 from functools import reduce

 l=[1,2,3,4,5]
print(reduce(lambda x,y:x+y,l)) ---
15

    - 示例2:

 from functools import reduce

 l=[1,2,3,4,5]
print(reduce(lambda x,y:x+y,l,1000)) # 指定初始值为 1000 ---
1015

  7.filter

    - filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回 True或 False,filter()根据判断结果自动过滤掉不符合条件的元素,返回由符合条件元素组成的新list;

     """
filter(function or None, iterable) --> filter object Return an iterator yielding those items of iterable for which function(item)
is true. If function is None, return the items that are true.
"""

    - 示例:

 l=['alex_SB','wupeiqi_SB','yuanhao_SB','egon']

 res=filter(lambda x:x.endswith('SB'),l)
print(res)
print(list(res)) ---
<filter object at 0x00000000006BA7F0>
['alex_SB', 'wupeiqi_SB', 'yuanhao_SB']

  8.global

    - 返回全局命名空间,比如全局变量名,全局函数名;

    - 在函数内使用global关键字,可以修改全局变量的值

 x=1000
def f1():
global x
x=0 f1()
print(x) ---
0

  9.进制相关

 print(bin(3))         # 二进制表示
print(hex(17)) # 十六进制表示
print(oct(9)) # 八进制表示 ---
0b11
0x11
0o11

  10.字符与数字转换

 print(ord("A"))                         # "A"字符对应的数值
print(chr(65)) # 数值65对应的字符 ---
65
A

  11.数学运算相关

 abs(-5)                          # 取绝对值,也就是5

 round(2.6)                       # 四舍五入取整,也就是3.0

 pow(2, 3)                        # 相当于2**3,如果是pow(2, 3, 5),相当于2**3 % 5

 cmp(2.3, 3.2)                    # 比较两个数的大小

 divmod(9,2)                      # 返回除法结果和余数

 max([1,5,2,9])                   # 求最大值

 min([9,2,-4,2])                  # 求最小值

 sum([2,-1,9,12])                 # 求和

  12.序列操作

 all([True, 1, "hello!"])         # 是否所有的元素都相当于True值

 any(["", 0, False, [], None])    # 是否有任意一个元素相当于True值

 sorted([1,5,3])                  # 返回正序的序列,也就是[1,3,5]

 reversed([1,5,3])                # 返回反序的序列,也就是[3,5,1]

  13.编译、执行

    - repr,返回对象的字符串表达;

     """
Return the canonical string representation of the object. For many object types, including most builtins, eval(repr(obj)) == obj.
"""

      - 参考:Python中的repr()函数

 >>>repr([0,1,2,3])
'[0,1,2,3]'
>>> repr('Hello')
"'Hello'" >>> str(1.0/7.0)
'0.142857142857'
>>> repr(1.0/7.0)
'0.14285714285714285'

    - compile,编译字符串成为code对象;

 >>> compile("print('Hello')",'test.py','exec')
<code object <module> at 0x0000000000A458A0, file "test.py", line 1>
>>>

    - eval,解释字符串表达式;

 cmd='print("你瞅啥")'
eval(cmd) dic="{'a':1,'b':2}"
print(type(dic),dic)
d=eval(dic)
print(type(d),d['a']) ---
你瞅啥
<class 'str'> {'a':1,'b':2}
<class 'dict'> 1

    - exec,解释并执行字符串;

 exec("print('Hello')")

 ---
Hello

  14.enumerate, 枚举,可设定初始值;

 res = enumerate([1,2,3],100)
print(type(res),res)
for item in res:
print(item) print('----------------')
S = 'cde'
for (index,char) in enumerate(S,1000):
print(index,char) ---
<class 'enumerate'> <enumerate object at 0x0000000000B24510>
(100, 1)
(101, 2)
(102, 3)
----------------
1000 c
1001 d
1002 e

  15.hash

 # 哈希
# 1. 只要校验的内容一致,那hash得到结果永远一样
# 2. 不可逆
# 3. 只要采用的哈希算法一样,那无论被校验的内容有多长,hash的到的结果长度都一样

    - 示例:

 print(hash('asdfasdfsadf'))
print(hash('asdfasdfsadf')) ---
5642351931168947998
5642351931168947998

  16.id

     """
Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects.
(CPython uses the object's memory address.)
"""

    - 示例:

 x=1
y=x
print(id(x))
print(id(y))
print(x is y) #判断的是身份 a=1
b=1
print(a == b) #判断的是值 ---
1757217232
1757217232
True
True

  17.format

    - 字符串格式化输出,推荐使用,不推荐使用 '%';

  18.slice

    - 构建下标对象 slice;

 l=['a','b','c','d','e']
print(l[1:4:2])
s=slice(1,4,2)
print(type(s),s)
print(l[s]) ---
['b', 'd']
<class 'slice'> slice(1, 4, 2)
['b', 'd']

  19.其他

 globals()                        # 返回全局命名空间,比如全局变量名,全局函数名

 locals()                         # 返回局部命名空间

 dir()                            # 用来查询一个类或者对象所有属性;

 help()                           # 用来查询的说明文档;

  20.内置函数列表

Python基础(协程函数、内置函数、递归、模块和包)-day05

参考:Python补充03 Python内置函数清单

四、面向过程编程与函数式编程

  1.面向过程编程

    1.概念:面向过程的程序设计:是一种流水线式的、机械式的过程;

    2.优缺点

      1.优点
        程序结构清晰,复杂的问题分解成多个简单的最小单元,每个单元完成各自的功能;
      2.缺点
        扩展性差,牵一发而动全身;
    3.应用场景
      Linux内核、git、httpd

  2.函数式编程

    1.函数式编程(不同于面向过程编程),是模拟数学意义上的函数;

    2.特性
      1.不允许对外部变量做任何修改
      2.没有循环的概念,所有的循环都是用 尾递归 实现的;
      3.函数式编程语言 与 Python无关;
      ...

五、模块的使用

  1.模块的概念

    - 一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀;

    - 一个大的项目往往包含多个功能,随着程序的逐渐发展,功能越来越丰富,为了方便管理,我们通常将程序分成一个个的文件,这样做程序的结构更清晰,方便管理;这时我们不仅仅可以把这些文件当做脚本去执行,还可以把他们当做模块来导入到其他的模块中,实现了功能的重复利用;

  2.模块的分类

    - 1.内置模块

    - 2.自定义模块

    - 3.第三方模块

  3.模块的使用

    - 1.import ... 导入

      - import spam,会将源文件的名称空间'spam'带到当前名称空间中,使用时必须是spam.名字的方式;

      - 执行文件:demo.py,模块文件:spam.py

      - 模块示例:

 #!/usr/bin/python
# -*- coding:utf-8 -*- #spam.py 模块
print('from the spam.py') money=1000 def read1():
print('spam->read1->money',money)
def read2():
print('spam->read2 calling read')
read1()
def change():
global money
money=0

      - 调用示例1:

 #!/usr/bin/python
# -*- coding:utf-8 -*- import spam spam.read2()
print(spam.money) ---
from the spam.py # import spam 这一行调用执行了 print('from the spam.py')
spam->read2 calling read # 可以通过 模块名 + '.' 的方式调用模块里的函数和变量
spam->read1->money 1000
1000

      - 调用示例2:

 #!/usr/bin/python
# -*- coding:utf-8 -*- import spam as x
x.read2() # 可以以别名的形式进行调用
print(x.money) ---
from the spam.py
spam->read2 calling read
spam->read1->money 1000
1000

      - 调用示例3:

 import spam

 money = 99
def read1():
print('----------->>>')
print(spam.money)
spam.read1()
read1() # read1() 与 spam.read1() 不冲突
print(money) # money 与 spam.money 不冲突 ---
from the spam.py
1000
spam->read1->money 1000
----------->>>
99

      - 调用示例4:

 #!/usr/bin/python
# -*- coding:utf-8 -*- import spam money = 9
print(spam.money)
spam.change() # 调用spam模块的change()函数,修改spam中全局空间的money值
print(spam.money)
print(money) # 执行文件中的money值没有被修改 ---
from the spam.py
1000
0
9

      - 导入模块干了那些事情? 

        - 1.为源文件(spam模块)产生新的名称空间;

        - 2.以新建的名称空间为全局名称空间,执行文件(spam.py)的代码;

          - 事实上函数定义也是“被执行”的语句,模块级别函数定义的执行将函数名放入模块全局名称空间表,用globals()可以查看;

        - 3.拿到一个模块名spam,指向spam.py产生的名称空间;

    - 2.from ... import ... 导入

      - from 语句相当于import,也会创建新的名称空间;但是将spam中的名字直接导入到当前的名称空间中,在当前名称空间中,直接使用名字就可以了;

      - 优点:方便,不用加前缀;

      - 缺点:容易跟当前文件的名称空间冲突;

      - 示例1:

 from spam import money,read1,read2,change

 print(money)
read1() # 在当前位置直接使用read1和read2就好了,执行时,仍然以spam.py文件全局名称空间;
change()
read1() ---
from the spam.py
1000
spam->read1->money 1000
spam->read1->money 0

      - 示例2:

 # 导入的函数read1,执行时仍然回到spam.py中寻找全局变量money
from spam import read1
money=20
read1() ---
from the spam.py
spam->read1->money 1000
 # 导入的函数read2,执行时需要调用read1(),仍然回到spam.py中找read1()
from spam import read2
def read1():
print('==========')
read2() ---
from the spam.py
spam->read2 calling read
spam->read1->money 1000

      - 示例3:

 # 导入的函数read1,被当前位置定义的read1覆盖掉了
from spam import read1
def read1():
print('==========')
read1() ---
from the spam.py
==========

      - 示例4:

 from spam import money,read1
money=200 # 将当前位置的名字money绑定到了100
print(money) # 打印当前的名字
read1() # 读取spam.py中的名字money,仍然为1000 ---
from the spam.py
200
spam->read1->money 1000

      - 示例5:

 # 也支持 as
from spam import read1 as read

      - 导入模块干了那些事情? 

        - 1.产生新的名称空间;

        - 2.以新建的名称空间为全局名称空间,执行文件的代码;

        - 3.直接拿到就是spam.py产生的名称空间中名字;

    - from ... import * 导入

      - 把spam中所有的不是以下划线(_)开头的名字都导入到当前位置;大部分情况下我们的python程序不应该使用这种导入方式,因为*你不知道你导入什么名字,很有可能会覆盖掉你之前已经定义的名字。而且可读性极其的差;

      - 示例1:

 from spam import *
print(money)
read2()
change()
read2() ---
from the spam.py
1000 # 打印了spam里的money值
spam->read2 calling read # 第一次调用 read2()
spam->read1->money 1000 # money --> 1000
spam->read2 calling read # 第二次调用 read2()
spam->read1->money 0 # money --> 0 ,因为这之前调用了 change(),修改了money在spam名称空间的值;

      - 示例2:这里有疑问!!!

 from spam import *
print(money)
change()
print(money) ---
from the spam.py
1000 # 第一次打印spam里的money值,然后紧接着调用了change()函数修改了money
1000 # 第二次打印money值,这是在修改之后的值,但是还是原来的值?????

      - 可以使用__all__来控制 *

      - __all__ 是和调用者里的 * 对应的;其他方式调用时无效;

 # 在spam.py 中新增加一行:__all__=['money','read1']
# 这样在另外一个文件中用from spam import * 就这只能导入列表中规定的两个名字 #!/usr/bin/python
# -*- coding:utf-8 -*- #spam.py
print('from the spam.py') money=1000 def read1():
print('spam->read1->money',money)
def read2():
print('spam->read2 calling read')
read1()
def change():
global money
money=0 __all__=['money','read1']

      - 示例3:

 from spam import *

 print(money)
read1() # 调用 spam.py 中 __all__ 中写好的函数和变量是没问题的; ---
from the spam.py
1000
spam->read1->money 1000

      - 示例4:

 from spam import *

 print(money)
read1()
change() # change 没有在 __all__ 里,所以调用这个会报错 ---
from the spam.py
1000
spam->read1->money 1000
Traceback (most recent call last):
File "D:/soft/work/Python_17/day05/模块/demo.py", line 75, in <module>
change()
NameError: name 'change' is not defined

  4.模块搜索路径

 python解释器在启动时会自动加载一些模块,可以使用sys.modules查看;

 在第一次导入某个模块时(比如spam),会先检查该模块是否已经被加载到内存中(当前执行文件的名称空间对应的内存),如果有则直接引用;
如果没有,解释器则会查找同名的内置模块;
如果还没有找到就从sys.path给出的目录列表中依次寻找spam.py文件。 所以总结模块的查找顺序是:
内存中已经加载的模块 --> 内置模块 --> sys.path路径中包含的模块
 import spam    ===>>   第一次导入,肯定不在内存中,也不是内置的,所以只能去 sys.path 中去找;

 如果在其他级别的路径下,则需要把该路径加入到 sys.path 

 import sys
sys.path.append(r'path')
sys.path.insert(0,r'path') 或者把相关的py文件移动到已有的sys.path某个路径下

    - 示例:查看和追加到sys.path

 import sys

 print(sys.path)
sys.path.append(r'D:\soft\work\Python_17\day06')
print(sys.path) ---
['D:\\soft\\work\\Python_17\\day05\\模块', 'D:\\soft\\work\\Python_17', 'D:\\soft\\work\\python35\\python35.zip', 'D:\\soft\\work\\python35\\DLLs', 'D:\\soft\\work\\python35\\lib', 'D:\\soft\\work\\python35', 'D:\\soft\\work\\python35\\lib\\site-packages']
['D:\\soft\\work\\Python_17\\day05\\模块', 'D:\\soft\\work\\Python_17', 'D:\\soft\\work\\python35\\python35.zip', 'D:\\soft\\work\\python35\\DLLs', 'D:\\soft\\work\\python35\\lib', 'D:\\soft\\work\\python35', 'D:\\soft\\work\\python35\\lib\\site-packages', 'D:\\soft\\work\\Python_17\\day06']

查看 sys.path

  5.模块的两种使用方式

    - 我们可以通过模块的全局变量 __name__ 来查看模块名;

    - 当做脚本直接执行时:

 #spam.py
print('from the spam.py')
money=1000 def read1():
print('spam->read1->money',money)
def read2():
print('spam->read2 calling read')
read1()
def change():
global money
money=0 print(__file__) # __file__ 就是文件名
print(__name__) # 当spam.py当做脚本直接执行时, __name__ 就是 __main__ ---
from the spam.py
D:/soft/work/Python_17/day05/模块/spam.py
__main__

    - 当做模块被其他人导入使用时:

 # demo.py, 导入spam模块,spam.py 就是上面那个

 import spam                       # 当spam.py当做模块被导入时,__name__ 就是 模块名,即spam

 ---
from the spam.py
D:\soft\work\Python_17\day05\模块\spam.py
spam

    - 应用:写好的模块代码都应该加上, if __name__ == '__main__' 这个代码块,然后把所有的有显示的操作都应该写在这里面;

      - 当自己测试/当做脚本执行的时候就可以正常执行并显示;

      - 当作为模块被其他人导入的时候也可以避免这些显示的操作被执行到;

    - 示例:

 #spam.py
print('from the spam.py')
money=1000 def read1():
print('spam->read1->money',money)
def read2():
print('spam->read2 calling read')
read1()
def change():
global money
money=0 if __name__ == '__main__':
print('This is spam module, running as scripts')

  6.dir()函数

    - 内建函数dir是用来查找模块中定义的名字,返回一个有序字符串列表;

 import spam
res = dir(spam)
print(type(res),res) import builtins
res2 = dir(builtins)
print(type(res2),res2) ---
from the spam.py
<class 'list'> ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'change', 'money', 'read1', 'read2']
<class 'list'> ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']

  7.扩展

    - 我们自定义的模块名不应该与系统内置模块重名;

    - 我们可以从 sys.modules 中找到当前已经加载的模块,sys.modules 是一个字典,内部包含模块名与模块对象的映射,该字典决定了导入模块时是否需要重新导入;

    - 考虑到性能的原因,每个模块只被导入一次,放入字典 sys.modules 中,如果你改变了模块的内容,你必须重启程序,python不支持重新加载或卸载之前导入的模块;

 import sys
import spam
print(sys.modules) ---
from the spam.py
{'nt': <module 'nt' (built-in)>, '_frozen_importlib_external': <module '_frozen_importlib_external' (frozen)>, '_sitebuiltins': <module '_sitebuiltins' from 'D:\\soft\\work\\python35\\lib\\_sitebuiltins.py'>, 'encodings.aliases': <module 'encodings.aliases' from 'D:\\soft\\work\\python35\\lib\\encodings\\aliases.py'>, 'sysconfig': <module 'sysconfig' from 'D:\\soft\\work\\python35\\lib\\sysconfig.py'>, 'builtins': <module 'builtins' (built-in)>, '_codecs': <module '_codecs' (built-in)>, 'errno': <module 'errno' (built-in)>, '_bootlocale': <module '_bootlocale' from 'D:\\soft\\work\\python35\\lib\\_bootlocale.py'>, '_multibytecodec': <module '_multibytecodec' (built-in)>, '_io': <module 'io' (built-in)>, 'io': <module 'io' from 'D:\\soft\\work\\python35\\lib\\io.py'>, 'stat': <module 'stat' from 'D:\\soft\\work\\python35\\lib\\stat.py'>, '_thread': <module '_thread' (built-in)>, 'encodings.latin_1': <module 'encodings.latin_1' from 'D:\\soft\\work\\python35\\lib\\encodings\\latin_1.py'>, 'encodings.gbk': <module 'encodings.gbk' from 'D:\\soft\\work\\python35\\lib\\encodings\\gbk.py'>, 'os.path': <module 'ntpath' from 'D:\\soft\\work\\python35\\lib\\ntpath.py'>, 'spam': <module 'spam' from 'D:\\soft\\work\\Python_17\\day05\\模块\\spam.py'>, 'site': <module 'site' from 'D:\\soft\\work\\python35\\lib\\site.py'>, 'abc': <module 'abc' from 'D:\\soft\\work\\python35\\lib\\abc.py'>, 'os': <module 'os' from 'D:\\soft\\work\\python35\\lib\\os.py'>, 'encodings.utf_8': <module 'encodings.utf_8' from 'D:\\soft\\work\\python35\\lib\\encodings\\utf_8.py'>, 'ntpath': <module 'ntpath' from 'D:\\soft\\work\\python35\\lib\\ntpath.py'>, '_codecs_cn': <module '_codecs_cn' (built-in)>, 'codecs': <module 'codecs' from 'D:\\soft\\work\\python35\\lib\\codecs.py'>, 'zipimport': <module 'zipimport' (built-in)>, '_signal': <module '_signal' (built-in)>, '_frozen_importlib': <module '_frozen_importlib' (frozen)>, '__main__': <module '__main__' from 'D:/soft/work/Python_17/day05/模块/demo.py'>, 'winreg': <module 'winreg' (built-in)>, 'genericpath': <module 'genericpath' from 'D:\\soft\\work\\python35\\lib\\genericpath.py'>, '_locale': <module '_locale' (built-in)>, '_weakrefset': <module '_weakrefset' from 'D:\\soft\\work\\python35\\lib\\_weakrefset.py'>, '_warnings': <module '_warnings' (built-in)>, 'sys': <module 'sys' (built-in)>, 'marshal': <module 'marshal' (built-in)>, 'encodings': <module 'encodings' from 'D:\\soft\\work\\python35\\lib\\encodings\\__init__.py'>, '_collections_abc': <module '_collections_abc' from 'D:\\soft\\work\\python35\\lib\\_collections_abc.py'>, '_imp': <module '_imp' (built-in)>, '_weakref': <module '_weakref' (built-in)>, 'encodings.mbcs': <module 'encodings.mbcs' from 'D:\\soft\\work\\python35\\lib\\encodings\\mbcs.py'>, '_stat': <module '_stat' (built-in)>}

查看当前 sys.modules

参考:egon老师总结的模块与包

六、包的使用

  1.包的概念

    - 包是包含 __init__.py 文件的文件夹;导入包就是导入包下的 __init__.py 文件;

    - 用途:从目录级别(文件夹级别)组织模块;

    - 包相当于一个大的模块,本质还是给使用者导入来使用的;

     导入包,是以包下 __init__.py 为准:
因为导入模块的时候需要创建新的名称空间;
但import 包,包是一个目录而非文件,所以就没办法创建新的名称空间,
所以需要在包下有一个 __init__.py 文件,创建新的名称空间; 导入的时候, '.' 的左边必须是包!!!
import bao.bao.file 导入的是一串,不仅仅是导入file,即会依次执行包下的__init__.py文件,可以实验验证:在包以及子包的__init__.py文件中加入print语句;见下面例子;

    - 示例,包名:glance

 glance/                   #Top-level package

 ├── __init__.py           #Initialize the glance package

 ├── api                   #Subpackage for api

 │   ├── __init__.py

 │   ├── policy.py

 │   └── versions.py

 ├── cmd                   #Subpackage for cmd

 │   ├── __init__.py

 │   └── manage.py

 └── db                    #Subpackage for db

     ├── __init__.py

     └── models.py

    - 文件内容

 # glance 的 __init__.py
print('from glance level.') # db 的 __init__.py
print('from glance -> db level.') # models.py
def register_models(engine):
print('from models.py: ',engine) print('from glance -> db -> models.') # policy.py
def get():
print('from policy.py') # versions.py
def create_resource(conf):
print('from version.py: ',conf) # manage.py
def main():
print('from manage.py')

    - 示例1:import ... 导入

 # packge.py ,和glance同级别的文件

 import glance.db.models           # 导入的是一串,不仅仅是models,所以可以看打印输出的结果;

 glance.db.models.register_models('mysql')
# models.register_models('mysql') # NameError: name 'models' is not defined 即不能只用 models. 调用,需要写全
---
from glance level.
from glance -> db level.
from glance -> db -> models.
from models.py: mysql

    - 示例2:支持 as 别名

 import glance.db.models as models

 models.register_models('mysql') 

 ---
from glance level.
from glance -> db level.
from glance -> db -> models.
from models.py: mysql

    - 示例3:from ... import ... 导入

 from glance.db import models                     # 可以导入模块
models.register_models('ldap') print('------------------>>>>>') from glance.db.models import register_models # 可以导入模块中的方法,一步到位;
register_models('mysql') ---
from glance level.
from glance -> db level.
from glance -> db -> models.
from models.py: ldap
------------------>>>>>
from models.py: mysql
# 凡是在导入时(而非使用时)带点的,点的左边都必须是一个包名;

# from ... import ... 方式导入,import后的东西必须是明确的不能带点,否则会有语法错误,如:from a import b.c是错误语法

# 不管是哪种方式,只要是第一次导入包或者是包的任何其他部分,都会依次执行包下的__init__.py文件(我们可以在每个包的文件内都打印一行内容来验证一下),这个文件可以为空,但是也可以存放一些初始化包的代码

    - 示例4:

 import glance

 glance.api.policy.get()        # 报错:找不到glance下的api

 ---
from glance level.
Traceback (most recent call last):
File "D:/soft/work/Python_17/day05/包/packge.py", line 21, in <module>
glance.api.policy.get()
AttributeError: module 'glance' has no attribute 'api'

      - 前面说过,导入包就是导入包下的 __init__.py,本例中实际上就是导入 glance下的 __init__.py 文件;

- 然而glance下__init__.py里面只有这一句:print('from glance level.'),所以肯定找不到 api,所以会报错!!!

    - 示例5:那怎么解决上面那个问题呢?既然import glance是导入执行下面的 __init__.py,那么我们就可以想到在这个 __init__.py 做一些操作了;

 # glance下的__init__.py文件与 api文件夹是同一个目录级别
# 所以我们想到在里面加一行:import api,如下:
import api
print('from glance level.') # packge.py ,即执行文件
import glance glance.api.policy.get() ---
Traceback (most recent call last):
File "D:/soft/work/Python_17/day05/包/packge.py", line 24, in <module>
import glance
File "D:\soft\work\Python_17\day05\包\glance\__init__.py", line 4, in <module>
import api
ImportError: No module named 'api' # 报错的地方是在 __init__.py里import api 这一步,说是找不到api,即路径找得不对;

    - 示例6:

 # 上面报错路径找得不对;这就回到了模块搜索路径的问题了:
# 内存 --> 内置 --> sys.path
# 很明显是 sys.path 没有找到api这个模块;
# 原因:执行文件是packge.py,而packge.py这个文件是和glance目录同级别的,而api是glance目录下的一个子目录,所以找不到;
# 验证这个想法:
# glance目录下的 __init__.py
print('from glance level.')
import sys
print(sys.path) # packge.py,即执行文件
import glance ---
from glance level.
['D:\\soft\\work\\Python_17\\day05\\包', 'D:\\soft\\work\\Python_17', 'D:\\soft\\work\\python35\\python35.zip', 'D:\\soft\\work\\python35\\DLLs', 'D:\\soft\\work\\python35\\lib', 'D:\\soft\\work\\python35', 'D:\\soft\\work\\python35\\lib\\site-packages'] # 很显然在 __init__.py里面打印的sys.path是执行文件packge.py的sys.path

      - 优化:

 # glance 下的 __init__.py
print('from glance level.')
import glance.api # packge.py
import glance print(glance.api) # 由打印结果可知,调用 glance.api 就是在执行 glance下api下的 __init__.py 文件 ---
from glance level.
from glance -> api level.
<module 'glance.api' from 'D:\\soft\\work\\Python_17\\day05\\包\\glance\\api\\__init__.py'>

      - 继续优化:

 # glance下的 api 下的 __init__.py
print('from glance -> api level.') import glance.api.policy # policy.py
def get():
print('from policy.py') # glance 下的 __init__.py
print('from glance level.')
import glance.api #packge.py,即执行文件,与glance目录同级
import glance print(glance.api)
print(glance.api.policy)
glance.api.policy.get() # 执行成功; ---
from glance level.
from glance -> api level.
<module 'glance.api' from 'D:\\soft\\work\\Python_17\\day05\\包\\glance\\api\\__init__.py'>
<module 'glance.api.policy' from 'D:\\soft\\work\\Python_17\\day05\\包\\glance\\api\\policy.py'>
from policy.py

    - 示例6:导入包就是导入包下的 __init__.py

 # 修改glance下的 __init__.py 文件
print('from glance level.') x = 1
dic = {
'k1':'v1',
'k2':'v2',
} # packge.py,即执行文件
import glance print(glance.x)
print(glance.dic) ---
from glance level.
1
{'k1': 'v1', 'k2': 'v2'}

    - 示例7:把子目录下的模块名字导入到glance的名称空间中

 # policy.py
def get():
print('from policy.py') # glance 下的 __init__.py
print('from glance level.') from glance.api import policy # packge.py
import glance # import glance 就是执行glance下的 __init__.py文件,即通过 from glance.api import policy 得到了 policy 这个模块名字,并且是属于glance名称空间的; glance.policy.get() # 所以可以直接用 glance.policy.get() 形式调用 policy模块里的方法; ---
from glance level.
from glance -> api level.
from policy.py

      - 进一步优化:直接使用 glance.get() ,省去中间的模块名,这样也极大的降低了使用者的学习成本;

 # glance 下的__init__.py
print('from glance level.') from glance.api.policy import get # 其实很简单,上个例子中是把policy这个模块名字拿到的 glance名称空间中,这里只要把policy下的get()方法拿到glance的名称空间中就可以了; # packge.py
import glance glance.get() ---
from glance level.
from glance -> api level.
from policy.py

  3.包的两种导入方式

    - 包的绝对导入

 # glance目录下的api目录下有 policy.py 和 verisions.py

 # policy.py
def get():
print('from policy.py') if __name__ == '__main__': # 直接执行 policy.py 用的
from versions import create_resource # 引用同目录级别的versions.py(policy.py 和 versions.py都在 api 子目录下)
create_resource('nginx.conf.inner')
else: # policy被使用者导入时用的
from glance.api.versions import create_resource # 必须要以 glance开头,因为包名就是 glance,即绝对导入
create_resource('nginx.conf') # versions.py
def create_resource(conf):
print('from version.py: ',conf) # glance下的 __init__.py
print('from glance level.') # packge.py,与glance同级的文件
import glance.api.policy ---
执行 packge.py 的结果:
from glance level.
from glance -> api level.
from version.py: nginx.conf 执行 policy.py 的结果:
from version.py: nginx.conf.inner

    - 包的相对导入,有问题???

 # packge.py 和glance同级别的执行文件
import glance.api.policy # glance下的 __init__.py
print('from glance level.') # policy.py (在api子目录下)
def get():
print('from policy.py') # api下的 __init__.py
print('from glance -> api level.') if __name__ == '__main__': # 作为执行文件,尝试导入db下的models模块
from ..db import models # 直接右键执行 policy.py 的时候,尝试去导入在 db 目录下的 model.py 模块;因为 db 和 api 这两个子目录是同一级别的,所以使用 ..db;
models.register_models('mysql.inner')
else: # 作为模块,被使用者导入
from ..db.models import register_models # 采用 .. 的方式导入,即相对导入;避免了因为改变包名导致包不可用的情况;
register_models('mysql') # db 下的 __init__.py
print('from glance -> db level.') # models.py (在 db 子目录下)
def register_models(engine):
print('from models.py: ',engine) print('from glance -> db -> models.') ---
# 执行 packge.py 的结果:
from glance level.
from glance -> api level.
from glance -> db level.
from glance -> db -> models.
from models.py: mysql # 执行 policy.py 的结果:
Traceback (most recent call last):
File "D:/soft/work/Python_17/day05/包/glance/api/policy.py", line 9, in <module>
from ..db import models
SystemError: Parent module '' not loaded, cannot perform relative import # 右键执行 policy.py 的时候,from ..db import models报错

      - 优化,即 将policy.py的上一级目录追加入到 sys.path里面:

 # 更新 policy.py 的代码
def get():
print('from policy.py') if __name__ == '__main__': # 作为执行文件
import os,sys
print(__file__) # 打印文件名
print(os.path.abspath(__file__)) # 打印文件的绝对路径
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 取得policy.py文件的上一级目录的绝对路径:policy.py所在路径是 xxx\glance\api\ , 上一级就是 xxx\glance
print(parent_dir)
sys.path.append(parent_dir) # 追加到 sys.path 列表中
print('------->>>')
print(sys.path)
print('------->>>')
from db import models # 追加完 xxx\glance 之后,即可以采用 from db import models 的方式导入,而不是 from ..db import models
models.register_models('mysql.inner')
else: # 作为模块,被其他人导入使用
from ..db.models import register_models # from ..db.models import register_models ,即包的相对导入
register_models('mysql') # models.py
def register_models(engine):
print('from models.py: ',engine) print('from glance -> db -> models.') # packge.py
import glance.api.policy ---
# 执行 packge.py 的结果:
from glance level.
from glance -> api level.
from glance -> db level.
from glance -> db -> models.
from models.py: mysql # 执行 policy.py 的结果:
D:/soft/work/Python_17/day05/包/glance/api/policy.py
D:\soft\work\Python_17\day05\包\glance\api\policy.py
D:\soft\work\Python_17\day05\包\glance
------->>>
['D:\\soft\\work\\Python_17\\day05\\包\\glance\\api', 'D:\\soft\\work\\Python_17', 'D:\\soft\\work\\python35\\python35.zip', 'D:\\soft\\work\\python35\\DLLs', 'D:\\soft\\work\\python35\\lib', 'D:\\soft\\work\\python35', 'D:\\soft\\work\\python35\\lib\\site-packages', 'D:\\soft\\work\\Python_17\\day05\\包\\glance']
------->>>
from glance -> db level.
from glance -> db -> models.
from models.py: mysql.inner

  4.包导入时的搜索路径

 导入时的搜索路径:内存 -> 内置 -> sys.path
注意:sys.path 是以执行文件为准的!!!
可以通过实验验证:test.py 导入一个包bbb里的一个文件a,bbb目录下还有b.py;这时候如果a.py要导入b.py就容易出问题
涉及到绝对导入和相对导入的问题:
包的绝对导入,以*包名为准
包的相对导入:. .. ...

  5. from ... import * 和 __all__=[]

    - 包结构:

 glance/                   #Top-level package

 ├── __init__.py           #Initialize the glance package

 ├── api                   #Subpackage for api

 │   ├── __init__.py

 │   ├── policy.py

 │   └── versions.py

 ├── cmd                   #Subpackage for cmd

 │   ├── __init__.py

 │   └── manage.py

 └── db                    #Subpackage for db

     ├── __init__.py

     └── models.py

glance包结构

    - 示例,结合之前的一些知识,再加上 __all__ 和 * ,做如下demo:

 # glance下的 __init__.py
print('from glance level.') # api 下的 __init__.py
print('from glance -> api level.') def func():
print('glance -> api -> __init__.py -> func()') __all__ = ['func','policy','versions'] # 包含了 __init__.py里的func, api下的policy和versions # db 下的 __init__.py
print('from glance -> db level.') # policy.py
def get():
print('from policy.py') if __name__ == '__main__':
import os,sys
print(__file__)
print(os.path.abspath(__file__))
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(parent_dir)
sys.path.append(parent_dir)
print('------->>>')
print(sys.path)
print('------->>>')
from db import models
models.register_models('mysql.inner')
else:
from ..db.models import register_models
register_models('mysql') # versions.py
def create_resource(conf):
print('from version.py: ',conf) # models.py
def register_models(engine):
print('from models.py: ',engine) print('from glance -> db -> models.') # packge.py # 执行文件
from glance.api import * # 导入glance.api下的 * ,即对应的是glance.api目录下的 __init__.py 里定义的 __all__ print('------>>>')
func() # 因为已经把 func、 policy 和 versions 导入了packge的名称空间,所以可以直接拿来执行
policy.get()
versions.create_resource('a.conf') ---
执行packge.py结果如下:
from glance level.
from glance -> api level.
from glance -> db level.
from glance -> db -> models.
from models.py: mysql
------>>>
glance -> api -> __init__.py -> func()
from policy.py
from version.py: a.conf

  - __all__中没定义的情况:

 # 如果 glance.api下的 __init__.py 里的__all__如下所示
__all__ = ['func','policy'] # packge.py
versions.create_resource('a.conf') ---
执行报错:
Traceback (most recent call last):
File "D:/soft/work/Python_17/day05/包/packge.py", line 41, in <module>
versions.create_resource('a.conf')
NameError: name 'versions' is not defined

七、练习

要求:

1     函数+正则===》计算器
2 -1+(1+2*3-(-4/3)+10.3/2)

代码实现:

参考:武Sir的计算器源码