装饰者接受任何论点

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了装饰者接受任何论点相关的知识,希望对你有一定的参考价值。

  1. def decorator_with_args(decorator_to_enhance) :
  2. """
  3. This function is supposed to be used as a decorator.
  4. It must decorate an other function, that is intended to be used as a decorator.
  5. Take a cup of coffee.
  6. It will allow any decorator to accept an arbitrary number of arguments,
  7. saving you the headache to remember how to do that every time.
  8. """
  9.  
  10. # We use the same trick we did to pass arguments
  11. def decorator_maker(*args, **kwargs) :
  12.  
  13. # We create on the fly a decorator that accepts only a function
  14. # but keeps the passed arguments from the maker .
  15. def decorator_wrapper(func) :
  16.  
  17. # We return the result of the original decorator, which, after all,
  18. # IS JUST AN ORDINARY FUNCTION (which returns a function).
  19. # Only pitfall : the decorator must have this specific signature or it won't work :
  20. return decorator_to_enhance(func, *args, **kwargs)
  21.  
  22. return decorator_wrapper
  23.  
  24. return decorator_maker

以上是关于装饰者接受任何论点的主要内容,如果未能解决你的问题,请参考以下文章

设计模式之装饰者模式

JS设计模式装饰者模式

设计模式之装饰者设计模式

设计模式-(14)装饰者模式 (swift版)

java 装饰着模式,谁能给我说明一下,谢谢了

设计模式 之 装饰者模式