Python学习Day3

1.装饰器:

定义:本质是函数,(功能装饰其他函数),就是为其他函数添加附近功能
原则:

     (1)不能修改被装饰的函数的源代码

     (2)不能修改被装饰的函数的调用方式

 1 import time
 2 
 3 def timmer(func):
 4     def warpper(*args,**kwargs):
 5         start_time = time.time()
 6         func()
 7         stop_time = time.time()
 8         print('the func run time is %s' % (stop_time - start_time))
 9     return warpper
10 @timmer
11 def test1():
12     time.sleep(3)
13     print('in the test1')
14     
15 test1()
View Code

实现装饰器知识储备:
  (1).函数即“变量”

  (2).高阶函数
       a:把一个函数名当做实参传递给另外一个函数
         (在不修改被装饰函数源代码的情况下为其添加功能)
       b:返回值中包含函数名
         (不修改函数的调用方式)

 1 #import time
 2 #
 3 #def bar():
 4 #    time.sleep(3)
 5 #    print('in the bar')
 6 #    
 7 #def test1(func):
 8 #    start_time = time.time()
 9 #    func()
10 #    stop_time = time.time()
11 #    print("the func run time is %s" % (stop_time - start_time))
12 #    
13 #test1(bar)
14 
15 import time
16 
17 def bar():
18     time.sleep(3)
19     print('in the bar')
20     
21 def test2(func):
22     print(func)
23     return func
24 
25 bar = test2(bar)
26 bar()
View Code

  (3).嵌套函数

1 def foo():                #嵌套函数
2     print('in the foo')
3     def bar():
4         print('in the bar')
5     bar()
6     
7 foo()
View Code

高阶函数+嵌套函数 =》 装饰器

 1 import time
 2 
 3 def timer(func):
 4     def deco():
 5         start_time = time.time()
 6     #    return func()          #return处函数直接结束了
 7         func()
 8         stop_time = time.time()
 9         print('the func run time is %s' % (stop_time - start_time))
10     return deco
11     
12 #def timer():
13 #    def deco():
14 #        pass
15     
16 @timer   #test1 = timer(test1)
17 def test1():
18     time.sleep(3)
19     print('in the test1')
20  
21 @timer   #test2 = timer(test2)
22 def test2():
23     time.sleep(3)
24     print('in the test2')
25     
26 test1()
27 test2()
28     
29 #test1 = deco(test1)
30 #test1()
31 #
32 #test2 = deco(test2)
33 #test2()  
34 
35 #print(timer(test1))
36 
37 #test1 = timer(test1)
38 #test1()
View Code

变量不使用了python会启动垃圾回收机制

2.生成器:

(1)一边循环一边计算的机制。

(2)有个next()的方法,列表加个()就变成了生成器

(3)把函数变成generator,使用yield即可。generator和函数的执行流程不一样。函数是顺序执行,遇到return语句或者最后一行函数语句就返回。而变成generator的函数,在每次调用next()的时候执行,遇到yield语句返回,再次执行时从上次返回的yield语句处继续执行

(4)使用yield实现在单线程的情况下实现并发运算的效果。

 1 import time
 2 
 3 def consumer(name):
 4     print("%s 准备吃包子啦!" % name)
 5     while True:
 6         baozi = yield
 7         print("包子[%s]来了,被[%s]吃了!" % (baozi,name))
 8 
 9 # c = consumer("ChenRonghua")
10 # c.__next__()
11 # b1 = "韭菜馅"
12 # c.send(b1)
13 # c.__next__()
14 
15 def producer(name):
16     c1 = consumer('A')
17     c2 = consumer('B')
18     c1.__next__()
19     c2.__next__()
20     print("老子开始准备做包子啦")
21     for i in range(10):
22         time.sleep(1)
23         print("做了两个包子!")
24         c1.send(i)
25         c2.send(i)
26 
27 producer("MM")
View Code

3.迭代器:

(1)可直接运用于for循环的对象称为iterable对象:

       a. 集合数据类型,list,tuple,dict,set,str都是iterable对象

       b. generator,包括生成器和带yield的function generator

(2)可以使用isinstance判断一个对象是否是一个Iterable对象,如;

>>>from collections import Iterable

>>>isinstance([],Iterable)

>>>True

(3)可以被next()函数调用并不断返回下一个值的对象称为迭代器,Iterator

(4)可以使用isinstance判断一个对象是否是一个Iterator对象,如:

>>> form collections import Iterator

>>>isinstance([],Iterator)

>>>False

(5)生成器都是Iterator对象,虽然list,tuple,dict,set,str都是Iterable,但都不是Iterator

(6)可以使用iter()函数将Iterable变成Iterator

4.BIF(内置函数)

 1 # print(all([1,-1,3]))    #可迭代对象中所有元素为真,结果为真
 2 # print(any([]))          #可迭代对象中只要有元素为真,结果为真
 3 # a = ascii([1,2,"梦梦"])
 4 # print(type(a),[a])
 5 # print(bin(8))             #表示成二进制的形式
 6 
 7 # a = bytes("abcde",encoding="utf-8")      #字节形不可修改
 8 # b = bytearray("abcde",encoding="utf-8")  #字节数组可以修改元素
 9 # print(b[1])
10 # b[1] = 50
11 # print(b)
12 # print(a.capitalize(),a)
13 
14 # def sayhi():pass
15 # print(callable(sayhi))    #判断是否可以调用,带括号的都可以调用
16 
17 # print(chr(100))           #输入数字得到相应的ascii的表
18 # print(ord('d'))           #输入ascii字符得到相应的数值
19 
20 # print(divmod(5,3))        #输入除数与被除数,返回商和余数
21 
22 # (lambda n:print(n))(5)
23 # calc = lambda n:print(n)
24 # calc(5)
25 
26 # res = filter(lambda n:n > 5,range(10))   #filter,过滤器
27 # res = map(lambda n:n**n,range(10))     #它接收一个函数 f 和一个 list,并通过把
28                                          #函数 f 依次作用在 list 的每个元素上,
29                                          # 得到一个新的 list 并返回
30 # for i in res:
31 #     print(i)
32 
33 # import functools
34 # res = functools.reduce(lambda x,y:x+y,range(10))
35 # print(res)
36 #reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,
37 # reduce()传入的函数 f 必须接收两个参数,reduce()对list的每个元素反复调用函数f,
38 # 并返回最终结果值。
39 
40 # b = frozenset([2,3,456,45,56,2])  #不可变集合
41 # print(globals()                   #返回这个文件中所有变量字典的形式
View Code
原文地址:https://www.cnblogs.com/mumupa0824/p/7486577.html