python 各种装饰器示例(python3)
参考网址:
Python中的各种装饰器详解_python_脚本之家
http://www.jb51.net/article/63892.htm
一、函数式装饰器:
1、装饰器无参数,被装饰对象无参数
1 def test(func): 2 def _test(): #_test函数内使用或返回func() 3 print('call the function {0}.'.format(func.__name__)) 4 return func() 5 return _test 6 7 @test 8 def say(): 9 print('hello,world') 10 11 say() 12 13 输出: 14 call the function say. 15 hello,world
2、装饰器无参数,被装饰对象有参数
1 def test(func): 2 def _test(*args,**kw): 3 print('call the function {0}.'.format(func.__name__)) 4 return func(*args,**kw) 5 return _test 6 7 @test 8 def say(str1,length): 9 print(str1[:length]) 10 11 say('hello,world',5) 12 13 输出: 14 call the function say. 15 hello
3、装饰器有参数,被装饰对象无参数
1 def arg_test(str_arg): 2 def test(func): 3 def _test(): 4 print('call the function {0}.'.format(func.__name__)) 5 print(str_arg) 6 return func() 7 return _test 8 return test 9 10 @arg_test('with arg') 11 def say(): 12 print('hello,world') 13 14 say() 15 16 输出: 17 call the function say. 18 with arg 19 hello,world
如果装饰器有默认参数,则用@arg_test(),无参数的装饰器直接用@arg_test
4、装饰器有参数,被装饰对象有参数
1 def arg_test(str_arg): 2 def test(func): 3 def _test(*args,**kw): 4 print('call the function {0}.'.format(func.__name__)) 5 print(str_arg) 6 return func(*args,**kw) 7 return _test 8 return test 9 10 @arg_test('with arg') 11 def say(str1,length): 12 print(str1[:length]) 13 14 say('hello,world',5) 15 16 输出: 17 call the function say. 18 with arg 19 hello
二、类装饰器
1、装饰器无参数,被装饰对象无参数
1 class test(object): 2 def __init__(self,func): 3 self._func = func 4 5 def __call__(self): 6 print('call the function {0}. '.format(self._func.__name__)) 7 return self._func() 8 9 @test 10 def say(): 11 print ('hello,world') 12 13 @test 14 def hehe(): 15 print('hehe') 16 17 say() 18 hehe() 19 20 输出: 21 call the function say. 22 hello,world 23 call the function hehe. 24 hehe
2、装饰器无参数,被装饰对象有参数
1 class test(object): 2 def __init__(self,func): 3 self._func = func 4 5 def __call__(self,*args,**kw): 6 print('call the function {0}. '.format(self._func.__name__)) 7 return self._func(*args,**kw) 8 9 @test 10 def say(str1,length): 11 print (str1[:length]) 12 13 say('hello,world',5) 14 15 输出: 16 call the function say. 17 hello
3、装饰器有参数,被装饰对象无参数
1 class test(object): 2 def __init__(self,info='de_arg'): 3 self._info = info 4 5 def __call__(self,func): 6 def __call(): 7 print (self._info) 8 print('call the function {0}. '.format(func.__name__)) 9 return func() 10 return __call 11 12 13 @test() 14 def say(): 15 print ('hello,world') 16 17 say() 18 19 输出: 20 de_arg
21 call the function say.
22 hello,world
3、装饰器有参数,被装饰对象有参数
1 class test(object): 2 def __init__(self,info='de_arg'): 3 self._info = info 4 5 def __call__(self,func): 6 def __call(*args,**kw): 7 print (self._info) 8 print('call the function {0}. '.format(func.__name__)) 9 return func(*args,**kw) 10 return __call 11 12 13 @test() 14 def say(str1,length): 15 print (str1[:length]) 16 17 say('hello,world',5) 18 19 输出: 20 de_arg 21 call the function say. 22 hello
三、防止装饰器修改函数名 __name_-
1 # import functools 2 3 def arg_test(str_arg): 4 def test(func): 5 # @functools.wraps(func) 6 def _test(): 7 print('call the function {0}.'.format(func.__name__)) 8 print(str_arg) 9 return func() 10 return _test 11 return test 12 13 @arg_test('with arg') 14 def say(): 15 print (say.__name__) 16 print('hello,world') 17 18 say() 19 20 输出: 21 call the function say. 22 with arg 23 _test #函数名被修改 24 hello,world
使用functools包
1 import functools 2 3 def arg_test(str_arg): 4 def test(func): 5 @functools.wraps(func) 6 def _test(): 7 print('call the function {0}.'.format(func.__name__)) 8 print(str_arg) 9 return func() 10 return _test 11 return test 12 13 @arg_test('with arg') 14 def say(): 15 print (say.__name__) 16 print('hello,world') 17 18 say() 19 20 输出: 21 call the function say. 22 with arg 23 say #函数名不变 24 hello,world