装饰器的主要作用:代码复用、装饰函数!
简单装饰器
现在有三个函数

每个函数都有自己的功能!
这时我想让这些函数在执行时都打印一句"Hello World!",当然!可以在每个函数内都添加一段print("Hello World!"),但是这里为了利用到装饰器我们往另一个方向想!
新建一个函数,专门用来打印"Hello World!"!所有函数调用时都得调用一次这个函数!
不过要怎么去实现嘞!

当然可以这么实现,不过我的目的是当调用foo、bar、bar1函数时再来调用这个函数!
像上图这么写的话就变成了调用pr_hell函数时再去调用其他函数!
这时就要想到装饰器了!利用装饰器就可以实现在调用其他函数时再去调用指定的函数(表面上看起来)!

如上图那样创建函数就可以很简单的创建一个装饰器函数了!
当要调用装饰器时!

没错,看到这里大概明白了一个装饰器的运转流程!
就是通过将原来的函数名引用传递给装饰器函数!然后装饰器建立一个内置函数后返回内置函数的引用!最后当我们调用foo函数时其实就调用了装饰器的内置函数了!
即对原来的函数名变量重定向!然后在新的函数内嵌套原来的函数, 最后再执行这个新的函数,就可以到达装饰原来函数的效果了!
通过打印两次foo的值可以很直观的看到foo指向的函数引用变了!

不过为了达到前面说的效果,还需要将函数名变量的重定向代码隐藏起来!
这时就可以利用装饰器的魔法糖@符号了!
只需要在要执行装饰器的函数前面加上@装饰器名即可让指定函数套上装饰器了!

源代码看起来就是这样了!

而执行了代码后,结果如下!

看起来就像魔法一样,明明我只调用了foo函数却执行了装饰器内的代码!
当然观察我输出了foo变量值可以看出,foo已经被重定向了!也就是说@符号的作用是重定向原来的函数名变量!
说白了装饰器其实就是利用了函数嵌套、函数变量重定向、return关键词等基本的python知识!最后再用@魔法糖符号来让这一切变得高大上而已!
现在理解了装饰器后, 就可以将装饰器玩弄起来了!
修改上面的装饰器,使其可以兼容任何函数(不带参数和带参数的)!
只需要利用不定长参数就可以很简单的实现了!

就像上面一样,这样不管需要套用的函数是否有参数都可以套用装饰器了!


为了加强理解我画了一张图!

带参数的装饰器
带参数的装饰器就是说装饰器函数带有参数!当然这个参数不是函数名!这样可以很大的提高装饰器的灵活性!不过需要修改一下装饰器的代码!

就像这样!可以看到最外层的装饰器函数需要传入一个参数!而原来的函数名需要传给第二层的内置函数!
因此我们需要这样重定向函数名变量!
foo = dec(True)(foo)
dec(True)可以返回pr_hello函数的引用!所以就变成了这样!
foo = pr_hello(foo)
跟不带参数的装饰器差别不大!而且利用了@魔法糖后也不需要自己赋值,只需要@dec(True)即可!



输出

装饰器函数基本上就是这样了!
不过装饰器函数有个缺点!就是原来函数的元信息都被代替掉了!

结果

可以看到foo函数的信息被wrapper函数代替掉了,这是当然的,因为foo已经指向了wrapper引用!
不过我们想要解决这个问题也是很简单的!
只需要导入wraps模块即可!wraps本身也是一个装饰器不过它的作用是copy传入函数的元信息到调用了这个装饰器的函数上!
然后让装饰器函数的内置函数调用wraps即可让内置函数的元信息变成原来函数的元信息!

结果

---------------------------------分割线---------------------------
希望各位大佬能指出错误的地方或者给些建议!
网友评论