1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 import time
 4 def cal(l):
 5     start_time=time.time()
 6     res=0
 7     for i in l:
 8         time.sleep(0.1)
 9         res+=1
10     stop_time=time.time()
11     print('函数的运行时间是%s'%(stop_time-start_time))
12     return res
13 print(cal(range(100)))

输出

函数的运行时间是10.07073187828064
100

 

装饰器

本质就是函数,功能是为其他函数添加附加功能。

  原则:

    不修改被修饰函数的源代码

    不修改被修饰函数的调用方式

  装饰器的知识储备

    装饰器=高阶函数+函数嵌套+闭包

    高阶函数:接收参数或返回值为函数的函数。

    函数嵌套:函数内又定义了函数。

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 def foo():
 4     time.sleep(3)
 5     print('来自foo')
 6 import time
 7 #多执行了一次,不合格
 8 def timer(func):
 9     start_time=time.time()
10     func()
11     stop_time=time.time()
12     print('函数运行时间是%s'%(stop_time-start_time))
13     return func
14 foo=timer(foo)
15 foo()

输出

来自foo
函数运行时间是3.000976085662842
来自foo

由此可见,只用高阶函数是实现不了装饰器的。

装饰器的框架

def timmer(func):
def wrapper():
print(func)
func()
return wrapper()

装饰器实现

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 import time
 4 def timmer(func): #func=test
 5     def wrapper(*args,**kwargs):
 6         # print(func)
 7         start_time=time.time()
 8         res=func(*args,**kwargs) #就是在运行test()
 9         stop_time = time.time()
10         print('运行时间是%s' %(stop_time-start_time))
11         return res
12     return wrapper
13 
14 @timmer #test=timmer(test)  要修饰哪个函数就在哪个函数前加
15 def test(name,age,gender):
16     time.sleep(3)
17     print('test函数运行完毕')
18     return '这是test的返回值'
19 res=test('linhaifeng',18,'male')
20 print(res)

输出

test函数运行完毕
运行时间是3.000012159347534
这是test的返回值

 

不用索引如何快速查询两边元素

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 l=[5,2,4,7,9,2,6,0,3]
4 a,b,*_,c,d=l
5 print(a,b,c,d)

输出

5 2 0 3

不用“桥梁”交换的快速方法

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 f1=1
4 f2=2
5 f1,f2=f2,f1
6 print(f1,f2)

输出

2 1

 

带参数验证功能装饰器

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 user_list=[
 4     {'name':'alex','passwd':'123'},
 5     {'name':'linhaifeng','passwd':'123'},
 6     {'name':'wupeiqi','passwd':'123'},
 7     {'name':'yuanhao','passwd':'123'},
 8 ]
 9 current_dic={'username':None,'login':False}
10 
11 def auth(auth_type='filedb'):
12     def auth_func(func):
13         def wrapper(*args,**kwargs):
14             print('认证类型是',auth_type)
15             if auth_type == 'filedb':
16                 if current_dic['username'] and current_dic['login']:
17                     res = func(*args, **kwargs)
18                     return res
19                 username=input('用户名:').strip()
20                 passwd=input('密码:').strip()
21                 for user_dic in user_list:
22                     if username == user_dic['name'] and passwd == user_dic['passwd']:
23                         current_dic['username']=username
24                         current_dic['login']=True
25                         res = func(*args, **kwargs)
26                         return res
27                 else:
28                     print('用户名或者密码错误')
29             elif auth_type == 'ldap':
30                 print('鬼才特么会玩')
31                 res = func(*args, **kwargs)
32                 return res
33             else:
34                 print('鬼才知道你用的什么认证方式')
35                 res = func(*args, **kwargs)
36                 return res
37 
38         return wrapper
39     return auth_func
40 
41 @auth(auth_type='filedb') #auth_func=auth(auth_type='filedb')-->@auth_func 附加了一个auth_type  --->index=auth_func(index)
42 def index():
43     print('欢迎来到京东主页')
44 
45 @auth(auth_type='ldap')
46 def home(name):
47     print('欢迎回家%s' %name)
48 #
49 @auth(auth_type='sssssss')
50 def shopping_car(name):
51     print('%s的购物车里有[%s,%s,%s]' %(name,'奶茶','妹妹','娃娃'))
52 
53 # print('before-->',current_dic)
54 # index()
55 # print('after--->',current_dic)
56 # home('产品经理')
57 shopping_car('产品经理')

输出

认证类型是 sssssss
鬼才知道你用的什么认证方式
产品经理的购物车里有[奶茶,妹妹,娃娃]

 

posted on 2018-09-20 18:52  阿缘要努力变强  阅读(163)  评论(0编辑  收藏  举报