Python- decorator (Decorator)

Thought decorator

  1. Decorator was based on the Python, a function can be passed as a parameter and return on the role of the function and behavior of the function to do the expansion and extension, but the process does not modify the logic of the original function, so called decorative rather than modify.
  2. Basically decorator logic:
    • Incoming objective function (func)
    • Create a new function (Wrapper)
    • In Wrapper perform func while adding extensions
    • Returns a new function Wrapper

      1. From the logic we see that behavior with a decorator is a function of expansion through new features to replace the original function, the process is done in code loads in the decorated function is actually decorator returned the new function of
      2. Since the new metadata decorative function has returned has changed, the new functions in order to maintain the same metadata and old function, may be used @ functools.wraps (f) a new decorative function Wrapper

Examples decorator

1. decorator with no arguments
2. decorator with parameters

Guess you like

Origin www.cnblogs.com/Peter2014/p/11264833.html