博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
heading python decorator
阅读量:6378 次
发布时间:2019-06-23

本文共 6201 字,大约阅读时间需要 20 分钟。


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_decoratordef 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...
decorator enhancer do something with args...(1, 2, 3){'test': 'test'}
func is decorated herewrapper with arg1 Liliy and arg2 Babydecorated function with func arg1 Liliy arg2Babydo something in decorated function
In [47]:
mydecorated_function("liliy", "funny")
 
this is decorator wrapper, before function...func_arg1 liliy func_arg2 funnythis is decorated function with arg1 liliy arg2 funnythis is decorator wrapper, after function

转载于:https://www.cnblogs.com/3days/p/5180178.html

你可能感兴趣的文章
简练软考知识点整理-项目成本管理简介
查看>>
局域网vlan划分
查看>>
大数据时代数据库-云HBase架构&生态&实践
查看>>
sqlServer sa账号被锁定
查看>>
mysql 源码安装
查看>>
Linux
查看>>
hypervisor的概念
查看>>
DVA
查看>>
2019第二届中国智慧零售终端大会
查看>>
PDF文件压缩方法,什么方法压缩PDF简单
查看>>
DHCP
查看>>
C++实现双进程守护
查看>>
区块链,目前人类史上最高的维度。
查看>>
我们在目前的21世纪,IT互联网能做什么,我们又该怎么去选择
查看>>
Oracle体系架构(一)--总介
查看>>
Oracle SQL的硬解析和软解析
查看>>
文件查找
查看>>
我的友情链接
查看>>
Linux关闭不必要的端口从而关闭不必要的服务
查看>>
鸡蛋学运维-6:nginx服务安装配置
查看>>