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('The run time of the function is%s'%(stop_time-start_time))
12 return res
13 print(cal(range(100)))
output
The run time of the function is 10.07073187828064
100
Decorator
The essence is function, and the function is to add additional functions for other functions.
Principle:
Do not modify the source code of the decorated function
Do not modify the call mode of the decorated function
Knowledge reserve of decorators
Decorator = higher order function + function nesting + closure
Higher order function: a function whose receiving parameter or return value is a function.
Function nesting: functions are defined within functions.
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 def foo(): 4 time.sleep(3) 5 print('Come from foo') 6 import time 7 #One more time, unqualified 8 def timer(func): 9 start_time=time.time() 10 func() 11 stop_time=time.time() 12 print('Function run time is%s'%(stop_time-start_time)) 13 return func 14 foo=timer(foo) 15 foo()
output
From foo
Function run time is 3.000976085662842
From foo
It can be seen that only higher-order functions can not realize the decorator.
Frame of decorator
def timmer(func):
def wrapper():
print(func)
func()
return wrapper()
Decorator implementation
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) #Is running test() 9 stop_time = time.time() 10 print('Run time is%s' %(stop_time-start_time)) 11 return res 12 return wrapper 13 14 @timmer #test=timmer(test) Before which function to modify 15 def test(name,age,gender): 16 time.sleep(3) 17 print('test Function completed') 18 return 'This is test Return value' 19 res=test('linhaifeng',18,'male') 20 print(res)
output
test function completed
The operation time is 3.000012159347534
This is the return value of test
How to query two side elements quickly without index
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)
output
5 2 0 3
A fast method of exchange without "bridge"
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 f1=1 4 f2=2 5 f1,f2=f2,f1 6 print(f1,f2)
output
2 1
Decorator with parameter verification function
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('The type of certification is',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('User name:').strip() 20 passwd=input('Password:').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('Wrong user name or password') 29 elif auth_type == 'ldap': 30 print('How can you play') 31 res = func(*args, **kwargs) 32 return res 33 else: 34 print('The devil knows what authentication method you use') 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 Attached a auth_type --->index=auth_func(index) 42 def index(): 43 print('Welcome to Jingdong Homepage') 44 45 @auth(auth_type='ldap') 46 def home(name): 47 print('Welcome home%s' %name) 48 # 49 @auth(auth_type='sssssss') 50 def shopping_car(name): 51 print('%s In the shopping cart[%s,%s,%s]' %(name,'Tea with milk','Younger sister','A doll')) 52 53 # print('before-->',current_dic) 54 # index() 55 # print('after--->',current_dic) 56 # home('product manager') 57 shopping_car('product manager')
output
The authentication type is SSSSSS
The devil knows what authentication method you use
In the product manager's shopping cart [milk tea, sister, doll]