Python Decorator Analysis

Source: Internet
Author: User
Tags python decorator

Decorator itself is a function, the function of which is to accept the modified function (decorated) as a parameter, return wrapper function (wrapper) to replace the modified function (decorated).

@decoratorfunc

Equal to func = decorator(func) . In most cases, the wrapper function must have the same parameters as the decorated function, so that the decorated function can be executed in the wrapper function and some extension processes are added. The principles based on this decorator are as follows:

    • Refer to the target function design for the parameters of the custom Decorator,wrapper function.
    • If the system decorator is used, because the wrapper function is clear, the target function is designed to refer to the parameters of the wrapper function.

Decorator accepts the target function as a parameter and returns the wrapper function, and therefore cannot accept additional arguments. In order to transfer additional parameters to the Decorator function, you can create the decorator maker function to accept the arguments and return the decorator function, then use decorator to complete the function substitution and replace the target function with the wrapper function.

Example:

  def mydecorator_maker (*args, **kwargs): print ' This is decorator maker function ... with args%s, Kwargs%s '        % (args, Kwargs) def mydecorator (func): print ' This is decorator ... with args%s, Kwargs%s '% (args, Kwargs) def decorator_wrapper (Func_arg1, func_arg2): print ' This is decorator wrapper, before function ... with ARG  1%s, arg2%s '% (Func_arg1, 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 Mydecora Ted_function (FUNC_ARG1, func_arg2): print ' This was decorated function with arg1%s, arg2%s '% (FUNC_ARG1, func_arg2) my Decorated_function (' Lily ', ' Baby ')  

Output:

This is decorator maker function ... with args (1, 2, 3), Kwargs {' Test ': ' Test '}

This is decorator ... with args (1, 2, 3), Kwargs {' Test ': ' Test '}

This is decorator wrapper, before function ... with arg1 Lily, arg2 Baby

This was decorated function with arg1 lily, arg2 Baby

This is decorator wrapper and after function

This is decorator maker function ...

This is done by using a maker function to accept additional parameters, and the process of doing some extra arguments is very concise. To expand, because of the features of the decorator function substitution, the decorator function can be decorator once to achieve the processing of additional parameters. First, comb the implementation logic:

    • The decorator principle is to replace the decorated function with the wrapper function. The wrapper function obtains the parameters of the decorated function, executes the decorated function, and then performs the business expansion process.
    • Decorated decorator function According to the above principle, and ordinary decorator is not different. Just add a decorator on the decorator to make it more capable of handling additional parameters.
    • DecoratorDecorator turns the original decorator function into a wrapper function that accepts parameters, and returns decorator after the function executes. Continue with the previous decorator process.
    • The wrapper function in the Decoratordecorator function differs from the normal wrapper function in that the decorator function is not executed, but the decorator function is returned. Therefore, it is not necessary to keep the parameters constant, which leads to additional parameter support.

      The inconsistent transformation of this parameter is contrary to the principle that the above wrapper parameters are consistent, but it still conforms to the basic function design of decorator function substitution.

Example :

  def decorator_decorator (decorator_to_decorated): print ' This decorator decorator'll decorate decorator pas    Sed in ... ' print ' passed in decorator '%s '% decorator_to_decorated def decorator_wrapper (*args, **kwargs): print ' Decor Ator wrapper do something with args%s, Kwargs%s ... '% (args, Kwargs) return decorator_to_decorated return deco Rator_wrapper@decorator_decoratordef Decorated_decorator (func): print ' func%s is decorated here '% func def Wrappe R (Func_arg1, FUNC_ARG2): print ' wrapper with arg1%s, Args2%s '% (FUNC_ARG1, func_arg2) return func (Func_ar    G1, FUNC_ARG2) return Wrapper@decorated_decorator (1, 2, 3, test= ' test ') def decorated_function (Func_arg1, FUNC_ARG2): print ' decorated function with func arg1%s, arg2%s '% (FUNC_ARG1, func_arg2) print ' Does something in decorated functi On ' decorated_function (' liliy ', ' Baby ')  

Output:

This decorator decorator would decorate decorator passed in ...

Passed in decorator are <function decorateddecorator at 0x111a99c80>

Decorator wrapper do something with args (1, 2, 3), Kwargs {' Test ': ' Test '} ...

Func <function decorated
function at 0x111b2bc08> are decorated here

Wrapper with Arg1 Liliy, Args2 Baby

Decorated function with func arg1 liliy, arg2 Baby

Do something in decorated function

Python Decorator Analysis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.