[关闭]
@zengxiankui 2017-08-04T13:11:20.000000Z 字数 16040 阅读 2061

Python装饰器分类

Python


装饰器函数: 是指装饰器本身是函数风格的实现;
函数装饰器: 是指被装饰的目标对象是函数;(目标对象);
装饰器类 : 是指装饰器本身是类风格的实现;
类装饰器 : 是指被装饰的目标对象是类;(目标对象);

一、装饰器函数:

1、目标对象是函数:

(1)、装饰器无参数:

A、目标无参数:

  1. strOldFunctionName = "";
  2. strNewFunctionName = "";
  3. #装饰器无参数:
  4. def decorator(callback): #装饰器函数/外部函数,它接受一个函数对象作为参数(这个函数一般就是目标函数);
  5. #目标无参数:
  6. def wrapper(): #闭包函数,用于传递目标函数的所有参数(没有参数);
  7. strOldFunctionName = callback.__name__;
  8. print "装饰前的函数名: %s" % strOldFunctionName;
  9. print "enter {}()".format(callback.__name__);
  10. callback(); #调用目标函数,执行原有功能;
  11. print "leave {}()".format(callback.__name__);
  12. pass;
  13. return wrapper; #返回闭包函数对象;
  14. @decorator #装饰器无参数;
  15. def target(): #目标函数:需要增加功能的函数(没有参数);
  16. strNewFunctionName = target.__name__;
  17. print "装饰后的函数名: %s" % strNewFunctionName;
  18. pass;
  19. target(); #用装饰过的新函数;
  20. print "函数名变化: %s --> %s" % (strOldFunctionName, strNewFunctionName)

B、目标有参数:

  1. strOldFunctionName = "";
  2. strNewFunctionName = "";
  3. #装饰器无参数:
  4. def decorator(callback): #装饰器函数/外部函数,它接受一个函数对象作为参数(这个函数一般就是目标函数);
  5. #目标有参数:
  6. def wrapper(*args, **kwargs): #闭包函数,用于传递目标函数的所有参数(任意参数);
  7. strOldFunctionName = callback.__name__;
  8. print "装饰前的函数名: %s" % strOldFunctionName;
  9. print "enter {}()".format(callback.__name__);
  10. callback(*args, **kwargs); #调用目标函数,执行原有功能;
  11. print "leave {}()".format(callback.__name__);
  12. pass;
  13. return wrapper; #返回闭包函数对象;
  14. @decorator #装饰器无参数;
  15. def target0(): #目标函数:需要增加功能的函数(没有参数) ;
  16. strNewFunctionName = target0.__name__;
  17. print "装饰后的函数名target0 = %s" % strNewFunctionName;
  18. pass;
  19. @decorator #装饰器无参数;
  20. def target1(a): #目标函数:需要增加功能的函数(1个参数) ;
  21. print "a = ", a;
  22. strNewFunctionName = target1.__name__;
  23. print "装饰后的函数名target1 = %s" % strNewFunctionName;
  24. pass;
  25. @decorator #装饰器无参数;
  26. def target2(a, b): #目标函数:需要增加功能的函数(2个参数) ;
  27. print "a = ", a, ", b = ", b;
  28. strNewFunctionName = target2.__name__;
  29. print "装饰后的函数名target2 = %s" % strNewFunctionName;
  30. pass;
  31. target0(); #调用装饰过的新函数;
  32. target1(6); #调用装饰过的新函数;
  33. target2(2, 8); #调用装饰过的新函数;

(2)、装饰器有参数:

A、目标无参数:

  1. strOldFunctionName = "";
  2. strNewFunctionName = "";
  3. #装饰器有参数:
  4. def decorator(name): #装饰器函数,参数name可以作为关键字使用(可选的特点);
  5. def wrapper(callback): #内嵌一级闭包函数wrapper(),用于传递目标函数对象;接受一个函数对象作为参数(这个函数一般就是目标函数);
  6. #目标无参数:
  7. def _wrapper(): #二级闭包函数_wrapper()用于传递目标函数的所有参数(没有参数);
  8. strOldFunctionName = callback.__name__;
  9. print "装饰前的函数名: %s" % strOldFunctionName;
  10. print "{name}: enter {func}()".format(name = name, func = callback.__name__); #打印输出:通过关键字模式"{name}"打印,关键字name与format()的name关键字参数相同,func雷同;
  11. callback(); #调用目标函数,执行原有功能(没有参数);
  12. print "{name}: leave {func}!".format(name = name, func = callback.__name__); #打印输出:通过关键字模式"{name}"打印,关键字name与format()的name关键字参数相同,func雷同;
  13. pass;
  14. return _wrapper; #在一级闭包函数中返回二级闭包函数对象;
  15. return wrapper; #在装饰器函数中返回一级闭包函数对象;
  16. #装饰器有参数:
  17. @decorator(name = "SYSTEM") #装饰目标函数,参数name被用作关键字参数传递(可选参数的特点);
  18. def target(): #目标无参数;
  19. strNewFunctionName = target.__name__;
  20. print "装饰后的函数名target = %s" % strNewFunctionName;
  21. pass;
  22. target(); #调用装饰过的新函数;

B、目标有参数:

  1. strOldFunctionName = "";
  2. strNewFunctionName = "";
  3. #装饰器有参数:
  4. def decorator(name): #装饰器函数,参数name可以作为关键字使用(可选的特点);
  5. def wrapper(callback): #内嵌一级闭包函数wrapper(),用于传递目标函数对象;接受一个函数对象作为参数(这个函数一般就是目标函数);
  6. #目标有参数:
  7. def _wrapper(*args, **kwargs): #二级闭包函数_wrapper()用于传递目标函数的所有参数;
  8. strOldFunctionName = callback.__name__;
  9. print "装饰前的函数名: %s" % strOldFunctionName;
  10. print "{name}: enter {func}()".format(name = name, func = callback.__name__); #打印输出:通过关键字模式"{name}"打印,关键字name与format()的name关键字参数相同,func雷同;
  11. callback(*args, **kwargs); #调用目标函数,执行原有功能;
  12. print "{name}: leave {func}!".format(name = name, func = callback.__name__); #打印输出:通过关键字模式"{name}"打印,关键字name与format()的name关键字参数相同,func雷同;
  13. pass;
  14. return _wrapper; #在一级闭包函数中返回二级闭包函数对象;
  15. return wrapper; #在装饰器函数中返回一级闭包函数对象;
  16. #装饰器有参数:
  17. @decorator(name = 'SYSTEM') #装饰目标函数,参数name被用作关键字参数传递(可选参数的特点);
  18. def target3(a, b, c):
  19. strNewFunctionName = target3.__name__;
  20. print "装饰后的函数名target = %s" % strNewFunctionName;
  21. print "a = %d, b = %d, c = %d" % (a, b, c);
  22. pass;
  23. #装饰器有参数:
  24. @decorator('PROCESS') #装饰目标函数,参数name没有被用作关键字参数传递;
  25. def target2(x, y):
  26. strNewFunctionName = target2.__name__;
  27. print "装饰后的函数名target = %s" % strNewFunctionName;
  28. print "x = %d, y = %d" % (x, y);
  29. pass;
  30. target2(6, 8); #调用装饰过的新函数;
  31. target3(4, 6, 8); #调用装饰过的新函数;

2、目标对象是类:

(1)、装饰器无参数:

A、目标无参数:

  1. strOldClassName = "";
  2. strNewClassName = "";
  3. #装饰器无参数:
  4. def decorator(cls): #装饰器,它没有参数,只是接受类对象作为参数(被装饰的目标类);
  5. #目标无参数:
  6. def wrapper(): #一级闭包函数,它负责传递类的构造函数需要用到的参数(没有参数);
  7. strOldClassName = cls.__name__;
  8. print "装饰前的类名: %s" % strOldClassName;
  9. print "call {name}.__init__".format(name = cls.__name__);
  10. objCls = cls(); #调用原始类的构造函数(没有参数);
  11. return objCls; #返回新的类对象(被装饰过的目标类对象);
  12. return wrapper; #返回一级闭包函数对象;
  13. @decorator
  14. class target:
  15. def __init__(self): #目标无参数;
  16. strNewClassName = target.__name__;
  17. print "装饰后的类名: %s" % strNewClassName;
  18. pass;
  19. def echo(self, msg):
  20. print "echo: ", msg;
  21. pass;
  22. t = target(); #用装饰过的新类创建对象;
  23. t.echo("XXXXXXXXXXXX");

B、目标有参数:

  1. strOldClassName = "";
  2. strNewClassName = "";
  3. #装饰器无参数:
  4. def decorator(cls): #装饰器,它没有参数,只是接受类对象作为参数(被装饰的目标类);
  5. #目标有参数:
  6. def wrapper(*args, **kwargs): #一级闭包函数,它负责传递类的构造函数需要用到的参数;
  7. strOldClassName = cls.__name__;
  8. print "装饰前的类名: %s" % strOldClassName;
  9. print "call {name}.__init__".format(name = cls.__name__);
  10. objCls = cls(*args, **kwargs); #调用原始类的构造函数;
  11. return objCls; #返回新的类对象(被装饰过的目标类对象);
  12. return wrapper; #返回一级闭包函数对象;
  13. @decorator
  14. class target1:
  15. def __init__(self, arg): #目标有参数;
  16. self.arg = arg;
  17. print "arg = ", arg;
  18. strNewClassName = target1.__name__;
  19. print "装饰后的类名: %s" % strNewClassName;
  20. pass;
  21. def echo(self, msg):
  22. print "echo: ", msg;
  23. pass;
  24. @decorator
  25. class target2:
  26. def __init__(self, arg1, arg2): #目标有参数;
  27. self.arg1 = arg1;
  28. self.arg2 = arg2;
  29. print "arg1 = ", self.arg1, ", arg2 = ", self.arg2;
  30. strNewClassName = target2.__name__;
  31. print "装饰后的类名: %s" % strNewClassName;
  32. pass;
  33. def echo(self, msg):
  34. print "echo: ", msg;
  35. pass;
  36. @decorator
  37. class target3:
  38. def __init__(self): #目标无参数;
  39. strNewClassName = target3.__name__;
  40. print "装饰后的类名: %s" % strNewClassName;
  41. pass;
  42. def echo(self, msg):
  43. print "echo: ", msg;
  44. pass;
  45. t1 = target1(123); #用装饰过的新类创建对象;
  46. t1.echo("1111111111");
  47. t2 = target2(456, 789); #用装饰过的新类创建对象;
  48. t1.echo("2222222222");
  49. t3 = target3(); #用装饰过的新类创建对象;
  50. t3.echo("3333333333");

(2)、装饰器有参数:

A、目标无参数:

  1. strOldClassName = "";
  2. strNewClassName = "";
  3. #装饰器有参数:
  4. def decorator(level = 'INFO'): #装饰器,它需要参数;
  5. def _wrapper(cls): #一级闭包函数对象,它接受一个类(被装饰的目标类)对象(类也是对象)作为参数;
  6. #目标无参数:
  7. def __wrapper(): #二级闭包函数,它负责传递类的构造函数需要用到的参数(没有参数);
  8. strOldClassName = cls.__name__;
  9. print "装饰前的类名: %s" % strOldClassName;
  10. print "[{level}] call {name}.__init__".format(level = level, name = cls.__name__);
  11. objCls = cls(); #调用原始类的构造函数(没有参数);
  12. return objCls; #返回新的类对象(被装饰过的目标类对象);
  13. return __wrapper; #返回二级闭包函数对象;
  14. return _wrapper; #返回一级闭包函数对象;
  15. @decorator()
  16. class target1:
  17. def __init__(self): #目标无参数;
  18. strNewClassName = target1.__name__;
  19. print "装饰后的类名: %s" % strNewClassName;
  20. pass;
  21. def echo(self, msg):
  22. print "echo: ", msg;
  23. pass;
  24. @decorator("DEBUG")
  25. class target2:
  26. def __init__(self): #目标无参数;
  27. strNewClassName = target2.__name__;
  28. print "装饰后的类名: %s" % strNewClassName;
  29. pass;
  30. def echo(self, msg):
  31. print "echo: ", msg;
  32. pass;
  33. @decorator(level = "SYSTEM")
  34. class target3:
  35. def __init__(self): #目标无参数;
  36. strNewClassName = target3.__name__;
  37. print "装饰后的类名: %s" % strNewClassName;
  38. pass;
  39. def echo(self, msg):
  40. print "echo: ", msg;
  41. pass;
  42. t1 = target1(); #用装饰过的新类创建对象;
  43. t1.echo("AAAAAAAA");
  44. t2 = target2(); #用装饰过的新类创建对象;
  45. t2.echo("BBBBBBBB");
  46. t3 = target3(); #用装饰过的新类创建对象;
  47. t3.echo("CCCCCCCC");

B、目标有参数:

  1. strOldClassName = "";
  2. strNewClassName = "";
  3. #装饰器有参数:
  4. def decorator(level = 'INFO'): #装饰器,它需要参数;
  5. def _wrapper(cls): #一级闭包函数对象,它接受一个类(被装饰的目标类)对象(类也是对象)作为参数;
  6. #目标有参数:
  7. def __wrapper(*args, **kwargs): #二级闭包函数,它负责传递类的构造函数需要用到的参数;
  8. strOldClassName = cls.__name__;
  9. print "装饰前的类名: %s" % strOldClassName;
  10. print "[{level}] call {name}.__init__".format(level = level, name = cls.__name__);
  11. objCls = cls(*args, **kwargs); #调用原始类的构造函数;
  12. return objCls; #返回新的类对象(被装饰过的目标类对象);
  13. return __wrapper; #返回二级闭包函数对象;
  14. return _wrapper; #返回一级闭包函数对象;
  15. @decorator()
  16. class target1:
  17. def __init__(self, arg):
  18. self.arg = arg;
  19. print "arg = ", arg;
  20. strNewClassName = target1.__name__;
  21. print "装饰后的类名: %s" % strNewClassName;
  22. pass;
  23. def echo(self, msg):
  24. print "echo: ", msg;
  25. pass;
  26. @decorator('ERROR')
  27. class target2:
  28. def __init__(self, arg1, arg2):
  29. self.arg1 = arg1;
  30. self.arg2 = arg2;
  31. print "arg1 = ", self.arg1, ", arg2 = ", self.arg2;
  32. strNewClassName = target2.__name__;
  33. print "装饰后的类名: %s" % strNewClassName;
  34. pass;
  35. def echo(self, msg):
  36. print "echo: ", msg;
  37. pass;
  38. @decorator(level = 'WARN')
  39. class target3:
  40. def __init__(self):
  41. strNewClassName = target3.__name__;
  42. print "装饰后的类名: %s" % strNewClassName;
  43. pass;
  44. def echo(self, msg):
  45. print "echo: ", msg;
  46. pass;
  47. t1 = target1(123); #用装饰过的新类创建对象;
  48. t1.echo("1111111111");
  49. t2 = target2(456, 789); #用装饰过的新类创建对象;
  50. t1.echo("2222222222");
  51. t3 = target3(); #用装饰过的新类创建对象;
  52. t3.echo("3333333333");

二、装饰器类:

装饰器本身是一个类,通过构造函数__init__()和回调函数__call__()实现装饰器功能;

1、目标对象是函数:

(1)、装饰器无参数:

A、目标无参数:

  1. #装饰器无参数:
  2. class decorator: #装饰器类,它也可以从object继承"class decorator(object)";
  3. def __init__(self, callback): #在构造函数里面接受callback对象(原始目标函数对象)作为参数;
  4. self.callback = callback;
  5. self.__name__ = callback.__name__; #保证被装饰之后函数名字不变;
  6. pass;
  7. #目标无参数:
  8. def __call__(self): #在__call__()函数中传递目标函数对象的所有参数(没有参数);
  9. print "装饰前的函数名: %s" % self.callback.__name__;
  10. print "enter {func}()".format(func = self.callback.__name__);
  11. result = self.callback();
  12. print "leave {func}()".format(func = self.callback.__name__);
  13. return result;
  14. @decorator
  15. def target():
  16. print "装饰后的函数名: %s" % target.__name__;
  17. pass;
  18. target(); #调用装饰过的新函数;

B、目标有参数:

  1. #装饰器无参数:
  2. class decorator: #装饰器类,它也可以从object继承"class decorator(object)";
  3. def __init__(self, callback): #在构造函数里面接受callback对象(原始目标函数对象)作为参数;
  4. self.callback = callback;
  5. self.__name__ = callback.__name__; #保证被装饰之后函数名字不变;
  6. pass;
  7. #目标有参数:
  8. def __call__(self, *args, **kwargs): #在__call__()函数中传递目标函数对象的所有参数(任意参数);
  9. print "装饰前的函数名: %s" % self.callback.__name__;
  10. print "enter {func}()".format(func = self.callback.__name__);
  11. result = self.callback(*args, **kwargs); #传递任意参数;
  12. print "leave {func}()".format(func = self.callback.__name__);
  13. return result;
  14. @decorator
  15. def target0():
  16. print "装饰后的函数名: %s" % target0.__name__;
  17. pass;
  18. @decorator
  19. def target1(a, b):
  20. print "a = %d, b = %d" % (a, b);
  21. print "装饰后的函数名: %s" % target1.__name__;
  22. pass;
  23. target0(); #调用装饰过的新函数;
  24. target1(6, 8); #调用装饰过的新函数;

(2)、装饰器有参数:

A、目标无参数:

  1. #装饰器有参数:
  2. class decorator:
  3. def __init__(self, name = 'INFO'): #在装饰器的构造函数中传递装饰器类需要的参数;
  4. self.name = name;
  5. pass;
  6. #目标无参数:
  7. def __call__(self, callback): #在__call__()函数中接受callback对象(原始目标函数对象)作为参数;
  8. def wrapper(): #内部闭包函数,给目标函数增加额外的功能(没有参数);
  9. print "装饰后的函数名: %s" % callback.__name__;
  10. print "[{name}] enter {func}()".format(name = self.name, func = callback.__name__);
  11. result = callback(); #调用原始目标函数,没有参数;
  12. print "[{name}] leave {func}()".format(name = self.name, func = callback.__name__);
  13. return result;
  14. return wrapper; #返回新的目标函数对象;
  15. @decorator()
  16. def target0():
  17. print "装饰后的函数名: %s" % target0.__name__;
  18. pass;
  19. @decorator('ERROR')
  20. def target1():
  21. print "装饰后的函数名: %s" % target1.__name__;
  22. pass;
  23. @decorator(name = 'SYSTEM')
  24. def target2():
  25. print "装饰后的函数名: %s" % target2.__name__;
  26. pass;
  27. target0(); #调用装饰过的新函数;
  28. target1(); #调用装饰过的新函数;
  29. target2(); #调用装饰过的新函数;

B、目标有参数:

  1. #装饰器有参数:
  2. class decorator:
  3. def __init__(self, name = 'INFO'): #在装饰器的构造函数中传递装饰器类需要的参数;
  4. self.name = name;
  5. pass;
  6. #目标有参数:
  7. def __call__(self, callback): #在__call__()函数中接受callback对象(原始目标函数对象)作为参数;
  8. def wrapper(*args, **kwargs): #内部闭包函数,给目标函数增加额外的功能(任意参数);
  9. print "装饰后的函数名: %s" % callback.__name__;
  10. print "[{name}] enter {func}()".format(name = self.name, func = callback.__name__);
  11. result = callback(*args, **kwargs); #调用原始目标函数,传递任意参数;
  12. print "[{name}] leave {func}()".format(name = self.name, func = callback.__name__);
  13. return result;
  14. return wrapper; #返回新的目标函数对象;
  15. @decorator()
  16. def target0():
  17. print "装饰后的函数名: %s" % target0.__name__;
  18. pass;
  19. @decorator('ERROR')
  20. def target1(a):
  21. print "装饰后的函数名: %s" % target1.__name__;
  22. print "a = %d" % (a);
  23. pass;
  24. @decorator(name = 'SYSTEM')
  25. def target2(x, y):
  26. print "装饰后的函数名: %s" % target2.__name__;
  27. print "x = %d, y = %d" % (x, y);
  28. pass;
  29. target0(); #调用装饰过的新函数;
  30. target1(2); #调用装饰过的新函数;
  31. target2(6, 8); #调用装饰过的新函数;

2、目标对象是类:

(1)、装饰器无参数:

A、目标无参数:

  1. #装饰器无参数:
  2. class decorator:
  3. def __init__(self, cls): #在装饰器的构造函数中传递被装饰类的对象;
  4. self.cls = cls;
  5. self.__name__ = cls.__name__; #保证被装饰之后类的名字不变;
  6. pass;
  7. #目标无参数:
  8. def __call__(self): #在__call__()函数中接受callback对象(原始目标函数对象)的所有参数(没有参数);
  9. print "装饰前的类名: %s" % self.cls.__name__;
  10. print "enter {func}()".format(func = self.cls.__name__);
  11. objCls = self.cls(); #调用原始类的构造函数,传递任意参数(没有参数);
  12. print "leave {func}()".format(func = self.cls.__name__);
  13. return objCls; #返回新的目标类对象;
  14. @decorator #装饰器无参数;
  15. class target:
  16. def __init__(self): #目标无参数;
  17. print "装饰后的类名: %s" % target.__name__;
  18. pass;
  19. def echo(self, msg):
  20. print "echo: ", msg;
  21. pass;
  22. t = target(); #用装饰过的新类创建对象;

B、目标有参数:

  1. #装饰器无参数:
  2. class decorator:
  3. def __init__(self, cls): #在装饰器的构造函数中传递被装饰类的对象;
  4. self.cls = cls;
  5. self.__name__ = cls.__name__; #保证被装饰之后类的名字不变;
  6. pass;
  7. #目标有参数:
  8. def __call__(self, *args, **kwargs): #在__call__()函数中接受callback对象(原始目标函数对象)的所有参数(任意参数);
  9. print "装饰前的类名: %s" % self.cls.__name__;
  10. print "enter {func}()".format(func = self.cls.__name__);
  11. objCls = self.cls(*args, **kwargs); #调用原始类的构造函数,传递任意参数;
  12. print "leave {func}()".format(func = self.cls.__name__);
  13. return objCls; #返回新的目标类对象;
  14. @decorator #装饰器无参数;
  15. class target0:
  16. def __init__(self): #目标无参数;
  17. print "装饰后的类名: %s" % target0.__name__;
  18. pass;
  19. def echo(self, msg):
  20. print "echo: ", msg;
  21. pass;
  22. @decorator #装饰器无参数;
  23. class target1:
  24. def __init__(self, arg1, arg2): #目标有参数;
  25. self.arg1 = arg1;
  26. self.arg2 = arg2;
  27. print "arg1 = ", self.arg1, ", arg2 = ", self.arg2;
  28. print "装饰后的类名: %s" % target1.__name__;
  29. pass;
  30. def echo(self, msg):
  31. print "echo: ", msg;
  32. pass;
  33. @decorator #装饰器无参数;
  34. class target2:
  35. def __init__(self, arg1): #目标有参数;
  36. self.arg1 = arg1;
  37. print "arg1 = ", self.arg1;
  38. print "装饰后的类名: %s" % target2.__name__;
  39. pass;
  40. def echo(self, msg):
  41. print "echo: ", msg;
  42. pass;
  43. t1 = target0(); #用装饰过的新类创建对象;
  44. t1.echo("AAAAAAAA");
  45. t2 = target1(6, 8); #用装饰过的新类创建对象;
  46. t2.echo("BBBBBBBB");
  47. t3 = target2(9); #用装饰过的新类创建对象;
  48. t3.echo("CCCCCCCC");

(2)、装饰器有参数:

A、目标无参数:

  1. #装饰器有参数:
  2. class decorator:
  3. def __init__(self, name = 'INFO'): #在装饰器的构造函数中传递装饰器类需要的参数;
  4. self.name = name;
  5. pass;
  6. #目标无参数:
  7. def __call__(self, cls): #在__call__()函数中接受callback对象(原始目标函数对象)作为参数;
  8. def wrapper(): #内部闭包函数,给目标类增加额外的功能(没有参数);
  9. print "装饰前的类名: %s" % cls.__name__;
  10. print "[{name}] enter {func}()".format(name = self.name, func = cls.__name__);
  11. objCls = cls(); #调用原始类的构造函数(没有参数);
  12. print "[{name}] leave {func}()".format(name = self.name, func = cls.__name__);
  13. return objCls;
  14. return wrapper; #返回新的目标函数对象;
  15. @decorator() #装饰器有参数;
  16. class target0:
  17. def __init__(self): #目标无参数;
  18. print "装饰后的类名: %s" % target0.__name__;
  19. pass;
  20. def echo(self, msg):
  21. print "echo: ", msg;
  22. pass;
  23. @decorator("DEBUG") #装饰器有参数;
  24. class target1:
  25. def __init__(self): #目标无参数;
  26. print "装饰后的类名: %s" % target1.__name__;
  27. pass;
  28. def echo(self, msg):
  29. print "echo: ", msg;
  30. pass;
  31. @decorator(name = "SYSTEM") #装饰器有参数;
  32. class target2:
  33. def __init__(self): #目标无参数;
  34. print "装饰后的类名: %s" % target2.__name__;
  35. pass;
  36. def echo(self, msg):
  37. print "echo: ", msg;
  38. pass;
  39. t1 = target0(); #用装饰过的新类创建对象;
  40. t1.echo("AAAAAAAA");
  41. t2 = target1(); #用装饰过的新类创建对象;
  42. t2.echo("BBBBBBBB");
  43. t3 = target2(); #用装饰过的新类创建对象;
  44. t3.echo("CCCCCCCC");

B、目标有参数:

  1. #装饰器有参数:
  2. class decorator:
  3. def __init__(self, name = 'INFO'): #在装饰器的构造函数中传递装饰器类需要的参数;
  4. self.name = name;
  5. pass;
  6. #目标有参数:
  7. def __call__(self, cls): #在__call__()函数中接受callback对象(原始目标函数对象)作为参数;
  8. def wrapper(*args, **kwargs): #内部闭包函数,给目标类增加额外的功能(任意参数);
  9. print "装饰前的类名: %s" % cls.__name__;
  10. print "[{name}] enter {func}()".format(name = self.name, func = cls.__name__);
  11. objCls = cls(*args, **kwargs); #调用原始类的构造函数,传递任意参数;
  12. print "[{name}] leave {func}()".format(name = self.name, func = cls.__name__);
  13. return objCls;
  14. return wrapper; #返回新的目标函数对象;
  15. @decorator() #装饰器有参数;
  16. class target0:
  17. def __init__(self): #目标无参数;
  18. print "装饰后的类名: %s" % target0.__name__;
  19. pass;
  20. def echo(self, msg):
  21. print "echo: ", msg;
  22. pass;
  23. @decorator("DEBUG") #装饰器有参数;
  24. class target1:
  25. def __init__(self, arg1, arg2): #目标有参数;
  26. self.arg1 = arg1;
  27. self.arg2 = arg2;
  28. print "arg1 = ", self.arg1, ", arg2 = ", self.arg2;
  29. print "装饰后的类名: %s" % target1.__name__;
  30. pass;
  31. def echo(self, msg):
  32. print "echo: ", msg;
  33. pass;
  34. @decorator(name = "SYSTEM") #装饰器有参数;
  35. class target2:
  36. def __init__(self, arg1): #目标有参数;
  37. self.arg1 = arg1;
  38. print "arg1 = ", self.arg1;
  39. print "装饰后的类名: %s" % target2.__name__;
  40. pass;
  41. def echo(self, msg):
  42. print "echo: ", msg;
  43. pass;
  44. t1 = target0(); #用装饰过的新类创建对象;
  45. t1.echo("AAAAAAAA");
  46. t2 = target1(6, 8); #用装饰过的新类创建对象;
  47. t2.echo("BBBBBBBB");
  48. t3 = target2(9); #用装饰过的新类创建对象;
  49. t3.echo("CCCCCCCC");

【备注】:

针对装饰器类用于装饰函数的情况,装饰器类还有如下写法:把内嵌的闭包函数定义成装饰器类的成员函数;
例如:

  1. #装饰器有参数:
  2. class decorator:
  3. def __init__(self, name = 'INFO'): #在装饰器的构造函数中传递装饰器类需要的参数;
  4. self.name = name;
  5. pass;
  6. #目标有参数:
  7. def __call__(self, callback): #在__call__()函数中接受callback对象(原始目标函数对象)作为参数;
  8. self.callback = callback;
  9. return self.wrapper; #返回新的目标函数对象(闭包函数对象);
  10. #原来的闭包函数被定义为成员函数:
  11. def wrapper(*args, **kwargs): #内部闭包函数,给目标函数增加额外的功能(任意参数);
  12. print "装饰后的函数名: %s" % callback.__name__;
  13. print "[{name}] enter {func}()".format(name = self.name, func = callback.__name__);
  14. result = self.callback(*args, **kwargs); #调用原始目标函数,传递任意参数;
  15. print "[{name}] leave {func}()".format(name = self.name, func = callback.__name__);
  16. return result;

【总结】:

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注