In Python, an adorner is used to extend some external functionality to a function at run time. However, in the process of use, because of the addition of adorners that cause the interpreter to think that the function itself has changed, in some cases-such as testing-will cause some problems. Python functool.wraps
solves this problem for us: When you write an adorner, adding it before implementation @functools.wraps(func)
ensures that the adorner does not affect the decorated function. For example, in Flask, we want to rewrite the login_required
adorner ourselves, but do not want to affect the way the adorner is decorated, the login_required
adorner itself can be written as follows:
def login_required_ (func): @wraps (func) def decorated_view (*args, **kwargs): if Current_app.login_ Manager._login_disabled: return func (*args, **kwargs) elif not current_user.is_authenticated: # return Current_app.login_manager.unauthorized () return redirect (Url_for ("Login.loginpage", Next=request.url)) return func (*args, **kwargs) return Decorated_view
The @functools used when implementing adorners in Python. Wraps reasons