Python 2-7 decorator

Python decorator @decorator ['dek ə re ɪ t ə ]

The Python function decorator accepts a function as a parameter, does some wrappers on the function without modifying the source code of the function, and then returns the wrapped function, that is, a new function is generated.

In the process of programming, we often encounter such scenarios: login verification, permission verification, logging, etc. These functional codes may be needed in various links, but they are very similar. The decorator is used to abstract and strip this part of the code. This kind of scenario can be solved very well.

def decorator_func(some_func):
    # define another wrapper function which modifies some_func
    def wrapper_func():
        print("Wrapper function started")   
        some_func()   
        print("Wrapper function ended")    

    return wrapper_func 
    # Wrapper function add something to the passed function and 

Guess you like

Origin blog.csdn.net/weixin_43955170/article/details/112916859