【python 】装饰器 (多个参数的函数,带参数的装饰器)【转】
最简单的模板是这样的
1 #-*-coding:utf-8-*- 2 3 def outer(func): 4 def inner(): 5 print 'before' 6 func() 7 print 'after' 8 # return r 9 return inner 10 ''' 11 def outer_1(func): 12 def inner(): 13 print 'before 11' 14 func() 15 print 'after 22' 16 # return r 17 return inner 18 19 @outer_1 20 ''' 21 @outer 22 def F1(): 23 print 'test' 24 25 if __name__ == '__main__': 26 F1() 27 ''' 28 运行结果 29 before 30 test 31 after 32 [Finished in 0.1s] 33 '''
函数带多个参数,装饰器对应修改以适合多种情况
# -*- coding: utf-8 -*- from time import ctime from time import sleep def ftfunc(func): def timef(*s,**gs): print "[%s] %s() called" % (ctime(),func.__name__) return func(*s,**gs) return timef @ftfunc def foo(*s,**gs): print(s) print(gs) if __name__ == '__main__': foo() foo(1) foo(1,2) foo(1,2,3) stu = {'name' : 'alam' ,'age' :12} foo(1,2,3,**stu) ''' 运行结果 [Wed Jun 14 15:49:55 2017] foo() called () {} [Wed Jun 14 15:49:55 2017] foo() called (1,) {} [Wed Jun 14 15:49:55 2017] foo() called (1, 2) {} [Wed Jun 14 15:49:55 2017] foo() called (1, 2, 3) {} [Wed Jun 14 15:49:55 2017] foo() called (1, 2, 3) {'age': 12, 'name': 'alam'} [Finished in 0.1s] '''
函数带多个参数,装饰器也带多个参数
# -*- coding: utf-8 -*- def decrator(*dargs, **dkargs): def wrapper(func): def _wrapper(*args, **kargs): print "decrator param:", dargs, dkargs print "function param:", args, kargs return func(*args, **kargs) return _wrapper return wrapper @decrator(1, a=2) def foo(x, y=0): print "foo", x, y if __name__ == '__main__': foo(3, 4) ''' 执行结果 decrator param: (1,) {'a': 2} function param: (3, 4) {} foo 3 4 [Finished in 0.1s] '''
函数带多个参数,装饰器能转换参数类型
1 # -*- coding: utf-8 -*- 2 3 def validate(**vkargs): 4 def decorator(func): 5 def wrapper(**kargs): 6 for key in vkargs: 7 # 根据vkargs中的参数的类型对kargs的参数进行类型转换 8 kargs[key] = vkargs[key](kargs[key]) 9 return func(**kargs) 10 return wrapper 11 return decorator 12 13 14 @validate(x=int, y=float, z=float) 15 def move(x, y, z): 16 print "move %d (%0.2f, %0.2f)"%(x, y, z) 17 18 19 20 if __name__ == '__main__': 21 move(x="1", y="0.2", z="0.3") 22 ''' 23 运行结果 24 move 1 (0.20, 0.30) 25 [Finished in 0.2s] 26 '''
一个简单的例子,登录页面
1 # -*- coding: utf-8 -*- 2 3 #模拟网站登陆访问认证 4 5 name = 'abc' 6 password='123' 7 8 def certi(model):#装饰器 9 def outr(fun):#装饰器加参数需要多加一层嵌套 10 def login(*args,**kwargs):#为了兼容各类函数参数,添加 *args,**kwargs 不固定参数 11 if model=='password': 12 print('enter password') 13 user_name = raw_input('user_name ').strip() 14 paswd=raw_input('password ').strip() 15 if user_name==name and paswd==password: 16 print('pass') 17 return fun(*args,**kwargs) 18 else: 19 print('error ') 20 exit() 21 elif model=='lamp': 22 print('this is lamp') 23 return fun(*args,**kwargs) 24 else: 25 print('error') 26 return login 27 return outr 28 29 30 def indxe(): 31 print('home page !!') 32 33 @certi(model='password') 34 def user(): 35 print('user page !!') 36 37 @certi(model='lamp') 38 def bbs(name): 39 print('bbs %s!!'%name) 40 41 42 43 if __name__ == '__main__': 44 indxe() 45 user() 46 bbs(name='yjj') 47 ''' 48 运行结果 49 home page !! 50 enter password 51 user_name abc 52 password 123 53 pass 54 user page !! 55 this is lamp 56 bbs yjj!! 57 '''