# python修饰器（装饰器）以及wraps

1、让调用方也就是ABC部门在调用的时候，先主动进行权限验证
2、S部门在对外提供的函数中，首先进行权限认证，然后再进行真正的函数操作

[En]

In the first option, the authority authentication that should not be exposed to the outer layer should be exposed before the use. At the same time, if there are multiple departments, everyone in each department should know that you are sure that others will do this, which is unreliable.

[En]

So, is there a way to solve this problem perfectly by following the principle of code opening and closing?

[En]

The answer must be yes, otherwise it would be really weak. Look at the code first.

## 装饰器知识点

[En]

After understanding the principle of the decorator, what is the timing of its execution? let’s take a look at it.

[En]

International practice, code first

[En]

When there are two or more decorators decorating a function, what is the execution process and decoration result? Again, the problem is illustrated by code.

1、装饰时机 通过上面装饰时机的介绍，我们可以知道，在执行到@makeBold的时候，需要对下面的函数进行装饰，此时解释器继续往下走，发现并不是一个函数名，而又是一个装饰器，这时候，@makeBold装饰器暂停执行，而接着执行接下来的装饰器@makeItalic，接着把test函数名传入装饰器函数，从而打印’b’，在makeItalic装饰完后，此时的test指向makeItalic的inner函数地址，这时候有返回来执行@makeBold，接着把新test传入makeBold装饰器函数中，因此打印了’a’。
2、在调用test函数的时候，根据上述分析，此时test指向makeBold.inner函数，因此会先打印’1’，接下来，在调用fun()的时候，其实是调用的makeItalic.inner()函数，所以打印’2’，在makeItalic.inner中，调用的fun其实才是我们最原声的test函数，所以打印原test函数中的’c’，’3’，所以在一层层调完之后，打印的结果为hello python decorator

[En]

In use, some functions may take arguments, so how to deal with this?

[En]

Specify that code comments are already available, so they will no longer be explained separately.

[En]

At this point, you may ask, that is a parameter, if there are multiple or indefinite length parameters, what to do? Take a look at the following code and you’ll see.

[En]

Let’s decorate the function with the return value. According to the previous writing, the code looks like this.

[En]

This achieves the expectation and finishes decorating the function with return parameters.

[En]

This paper introduces the decoration of functions with parameters and functions with return values, so is there a decorator with parameters, and if so, what is the use?

[En]

The answer must be yes, let’s take a look at it through the code.

[En]

The advantage is that different application functions can be processed according to different parameters at run time.

[En]

Introduced so much, in practical application, if there is no category of functions to write a decorator, it is estimated to be exhausted, then there is no universal universal decorator, the answer must be yes, no more nonsense, directly on the code.

[En]

Combine the above examples to complete the function of the universal decorator, the principle is the same as above, it is not too much nonsense.

## 类装饰器

[En]

Next, let’s get to the point and take a look at how to decorate functions with classes.

## update_wrapper

[En]

First of all, let’s write a custom modifier, which has no function, only a document string, as follows:

OK，至此，我们已经了解了 partialupdate_wrapper这两个函数的功能，接下来我们翻出 wraps修饰器的源码：

## 自定义修饰器v3

https://segmentfault.com/a/1190000009398663

https://blog.csdn.net/u010358168/article/details/77773199

Original: https://www.cnblogs.com/slysky/p/9777424.html
Author: 勇敢的公爵
Title: python修饰器（装饰器）以及wraps

(0)