decorator make a wrapper function do something before and after the original function. The wrapper function share arguments with original function.@decorator is same with func = decorator(func); decorator receive func and return a wrapper func with same arguments with original func. The decorator function is done in @decorator progress. Wrapper fucntion in decorator share the same argument with original function and run the original function in itself. If you want decotrator receive some arguements(as decorator receiv function arguemnt return arguments funcion), you need the decotrator return a maker function which receive argument and return wrapper function(same arguments with original function). The sample is as follows.

  • so a maker functions will return wrapper function with arguments same with original fucntion.
  • We user maker functions to receive arguments for wrapper functions. So wrapper functions can do something with some other arguments.
In [67]:
def mydecorator_maker(*args, **kwargs):
print "this is decorator maker function..."
print args, kwargs
def mydecorator(func):
print "this is decorator... args is passed here"
print args, kwargs
def decorator_wrapper(func_arg1, func_arg2):
print "this is decorator wrapper, before function..."
print "func_arg1 " + func_arg1 + " func_arg2 " + func_arg2
func(func_arg1, func_arg2)
print "this is decorator wrapper, after function"
return decorator_wrapper
return mydecorator @mydecorator_maker(1, 2, 3, test="test")
def mydecorated_function(func_arg1, func_arg2):
print "this is decorated function with arg1 " + func_arg1 + " arg2 " + func_arg2
 
this is decorator maker function...
(1, 2, 3) {'test': 'test'}
this is decorator... args is passed here
(1, 2, 3) {'test': 'test'}
 

This is another example for decorated doecorator. It works also with maker and with interation. Nothing special from above maker function

In [1]:
def decorator_with_args(decorator_to_enhance):
"""
This function is supposed to be used as a decorator.
It must decorate an other function, that is intended to be used as a decorator.
Take a cup of coffee.
It will allow any decorator to accept an arbitrary number of arguments,
saving you the headache to remember how to do that every time.
""" # We use the same trick we did to pass arguments
def decorator_maker(*args, **kwargs): # We create on the fly a decorator that accepts only a function
# but keeps the passed arguments from the maker.
def decorator_wrapper(func): # We return the result of the original decorator, which, after all,
# IS JUST AN ORDINARY FUNCTION (which returns a function).
# Only pitfall: the decorator must have this specific signature or it won't work:
return decorator_to_enhance(func, *args, **kwargs) return decorator_wrapper return decorator_maker
In [2]:
# You create the function you will use as a decorator. And stick a decorator on it :-)
# Don't forget, the signature is "decorator(func, *args, **kwargs)"
@decorator_with_args
def decorated_decorator(func, *args, **kwargs):
def wrapper(function_arg1, function_arg2):
print "Decorated with", args, kwargs
return func(function_arg1, function_arg2)
return wrapper # Then you decorate the functions you wish with your brand new decorated decorator. @decorated_decorator(42, 404, 1024)
def decorated_function(function_arg1, function_arg2):
print "Hello", function_arg1, function_arg2 decorated_function("Universe and", "everything")
#outputs:
#Decorated with (42, 404, 1024) {}
#Hello Universe and everything # Whoooot!
 
Decorated with (42, 404, 1024) {}
Hello Universe and everything
In [3]:
decorated_decorator = decorator_with_args(decorated_decorator)
 

I will use a decorated docorator to receive arguments instend of maker functions. I think this is a really realization of intetraion.

  • Decorator means a function change from orginal function to wrapper function in decorator function.
  • The decorater function here will be change to a function do something with arguments and return the deocrator(which change original fucntion to wrapper).
  • We can make this change by a decorated decorator.
  • The decorated decorator receive decorator return a enhancer function dothing something with arguments. The function return the decorator at last. The enchancer function provide a enhacement to decorator with ablity to do something with some arguments.
In [71]:
def decorator_enhance_decorator(decorator_to_enhancement):
print "This decorated decorator will enhance decorator function..."
print decorator_to_enhancement
#def decorator_enhancer(*args, **kwargs):
def decorator_enhancer(*args, **kwargs):
print "decorator enhancer do something with args..."
print args
print kwargs
return decorator_to_enhancement
return decorator_enhancer
#return decorator_enhance_maker @decorator_enhance_decorator
def decorated_decorator3(func):
print func
print "func is decorated here"
def wrapper(func_arg1, func_arg2):
print "wrapper with arg1 " + func_arg1 + " and arg2 " + func_arg2
return func(func_arg1, func_arg2)
return wrapper @decorated_decorator3(1, 2, 3, test="test")
def decorated_function3(func_arg1, func_arg2):
print "decorated function with func arg1 " + func_arg1 + " arg2" + func_arg2
print "do something in decorated function" decorated_function3("Liliy", "Baby")
 
This decorated decorator will enhance decorator function...
<function decorated_decorator3 at 0x10b7755f0>
decorator enhancer do something with args...
(1, 2, 3)
{'test': 'test'}
<function decorated_function3 at 0x10b775668>
func is decorated here
wrapper with arg1 Liliy and arg2 Baby
decorated function with func arg1 Liliy arg2Baby
do something in decorated function
In [47]:
mydecorated_function("liliy", "funny")
 
this is decorator wrapper, before function...
func_arg1 liliy func_arg2 funny
this is decorated function with arg1 liliy arg2 funny
this is decorator wrapper, after function

最新文章

  1. javaweb-url /
  2. SQL 简单查询语句 select
  3. UNIX高级环境编程学习
  4. java编程acm基础
  5. FFTW库在VS 2010中的使用方法
  6. 【转】Github轻松上手5-站在巨人的肩膀上(Fork)
  7. 八,WPF 命令
  8. Jquery Mobile学习
  9. TCP/IP详解之:IP选路 动态选路协议
  10. System.InvalidOperationException: 找到多个与名为“Home”的控制器匹配的类型。
  11. Python基础之字符串
  12. FormsAuthentication.HashPasswordForStoringInConfigFile 的替代方法
  13. 如何修改WinPE Boot的.wim镜像文件
  14. JSP项目前端优化
  15. 学习笔记之X分钟速成Python3
  16. openal在vs2010中的配置
  17. app 性能
  18. Oracle列转行函数LISTAGG()
  19. js 基础-&amp;&amp; || 逻辑与和逻辑或
  20. 第16月第6天 vs2005 lseek directdraw

热门文章

  1. [转]常用的快速Web原型图设计工具
  2. Webservice接口
  3. selenium3.0.1调用firefox
  4. javascript运行模式:并发模型 与Event Loop
  5. IE8下服务端获取客户端文件的路径为C:/fakePath问题的解决方案
  6. python基础学习笔记3
  7. iOS视频播放器
  8. 基于VLC的视频播放器(转载)
  9. ArcEngine读取数据(数据访问) (转)
  10. c# 的MD5加密算法