回顾下上次的内容
![](https://images2015.cnblogs.com/blog/771858/201608/771858-20160814103243859-1339411925.png)
下面开始讨论装饰器
先写一个基础函数:
所以装饰器的原则:
先来看函数式怎么定义,怎么运行的:
函数只有在调用运行的时候才会去找定义的函数:
函数体 就是一堆字符串,没有任何功能,我们在定义的时候只是把这一堆字符串放到了一个内存空间里。
这里说一下解释器回收内存的机制:
门牌号没有的情况下就会清理房间
当然也有匿名函数:
没有引用,立刻删除
高阶函数的使用案例:
所以是这样写的:
说到这里我们要复习一下作用域:
真正开始装饰器:
把deco的内存地址赋予给原来的函数,再运行deco 函数
总结一下:
装饰器的传入参数:
实践: 这种的装饰器可以满足日常90% 的需求了。
1 #!/usr/bin/env python3 2 # Auth: Shen Yang 3 import time 4 #基础 5 #------------------- 6 def test1(): 7 time.sleep(2) 8 print("in the test1") 9 #test1() 10 def test2(name): 11 print("hehe",name) 12 #test2() 13 #------------------- 14 #定义一个装饰器函数 15 def timer(func): #定义一个传入参数 16 def deco(*args,**kwargs): #定义一个内嵌函数,实现新功能 17 start_time = time.time() 18 func(*args,**kwargs) 19 stop_time = time.time() 20 print("The func run time is {_time}".format(_time=stop_time-start_time)) 21 return deco #返回deco 临时函数的内存地址,从外面调用 22 @timer #test1 =timer(test1) 23 def test1(): 24 time.sleep(2) 25 print("in the test1") 26 @timer #test2 =timer(test2("Wang Lu")) 27 def test2(name): 28 print("hehe",name) 29 test1() 30 test2("Wang Lu")
![](https://images2015.cnblogs.com/blog/771858/201608/771858-20160819101806125-150915687.png)
把列表生成器变成一个生成器
![](https://images2015.cnblogs.com/blog/771858/201608/771858-20160819101819625-1625503530.png)
把函数改为生成器:
异常处理:
生成器
1 list2 = (i*3 for i in range(10)) 2 print(list2.__next__()) 3 print("chulai1") 4 print(list2.__next__()) 5 print("chulai2") 6 print(list2.__next__()) 7 print("chulai3") 8 print(list2.__next__())
函数:
1 def gent(max): 2 n,a,b = 0,0,1 3 while n < max: 4 print(b) 5 a,b = b,a + b 6 n = n + 1 7 return "Over" 8 gent(10)
变成生成器:
1 def gent(max): 2 n,a,b = 0,0,1 3 while n < max: 4 yield (b) 5 a,b = b,a + b 6 n = n + 1 7 return "Over" 8 flb = gent(10) 9 print(flb.__next__()) 10 print(flb.__next__()) 11 print(flb.__next__()) 12 print(flb.__next__())
使用return 返回错误:
1 def gent(max): 2 n,a,b = 0,0,1 3 while n < max: 4 yield (b) 5 a,b = b,a + b 6 n = n + 1 7 return "Over" 8 flb = gent(10) 9 while True: 10 try: 11 a = flb.__next__() 12 print("this is ",a) 13 except StopIteration as err: 14 print("Error in ",err.value) 15 break
![](https://images2015.cnblogs.com/blog/771858/201608/771858-20160819102209328-1780116425.png)
1 def consumer(name): 2 print("{_name}准备吃包子哈".format(_name=name)) 3 while True: 4 baozi = yield 5 print("做出了{_baozi}的包子,被{_name}吃啦".format(_baozi=baozi,_name=name)) 6 7 c = consumer("Wanglu") 8 c.__next__() 9 c.send("韭菜馅") #和__next__ 效果一样,但是给 yield 传入了参数
1 import time 2 def consumer(name): 3 print("{_name}准备吃包子哈".format(_name=name)) 4 while True: 5 baozi = yield 6 print("做出了{_baozi}的包子,被{_name}吃啦".format(_baozi=baozi,_name=name)) 7 def producer(): 8 c = consumer("王璐") 9 c1 = consumer("申嘉锐") 10 c.__next__() 11 c1.__next__() 12 print("开始做包子了!!!") 13 for i in range(1,10): 14 time.sleep(1) 15 print("做了个包子,分两半来吃吧") 16 c.send(i) 17 c1.send(i) 18 producer()
![](https://images2015.cnblogs.com/blog/771858/201608/771858-20160819102327468-587857394.png)
迭代器 和 可迭代对象 不是一回事
把 list dic str 变为迭代器
any() 任意一个为真就返回真,如果为空返回False
ascii() 吧内存的对象转换为可打印的字符串
bin() 十进制整数转为二进制 有用
bytes()
bytearray() 可修改的二进制
callable() 判断是否可调用
chr() 返回aiscll 码对应的字符
ord() 返回aiscll 位置对应的序号
compile()
![](https://images2015.cnblogs.com/blog/771858/201608/771858-20160819102623125-1083600473.png)
filter() 一组数据中过滤出满足条件数据。
map() 对传入的每一个值进行处理,返回相应的结果 和列表生成器 一样的
reduce() 需要import functools
frozenset() 编程一个不可变集合
globals() 以key value 形式返回整个文件中变量
![](https://images2015.cnblogs.com/blog/771858/201608/771858-20160819102801093-1731803897.png)
hex() 16进制转换
![](https://images2015.cnblogs.com/blog/771858/201608/771858-20160819102832500-1849779424.png)
zip() 拉链,一拉就和到一起了
如果有多的或者少的按少的算:
__import__() 非常有用: 导入字符串的模块
![](https://images2015.cnblogs.com/blog/771858/201608/771858-20160819102936796-330164680.png)
序列化:
反序列化:
json 只能处理简单的数据类型,如函数啊 类啊 就报错
找其他目录的程序并运行
绝对路径:
动态添加环境变量,才能import