概述
<div class="markdown-here-wrapper" data-md-url="https://i.cnblogs.com/EditPosts.aspx?opt=1">
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">说起装饰器我们可能已经很熟悉了(不了解的可以查看<a href="https://www.cnblogs.com/sfencs-hcy/p/9615651.html">python基础学习——装饰器),随手就可以写一个简单的装饰器
:
301_1595@):
函数前做点事
result = func(*args,**@R_301_1595@)
函数后干点啥
result
inner
函数的方式实现的,但我们也可以用类的方式写一个同样的装饰器,还记得方法吗
:
__init__:
self.func = func
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def</span> <span class="hljs-title" style="color: #7285b7;">__call__</span><span class="hljs-params" style="color: #ffc58f;">(self,*args,**@R_<a href="https://www.jb51.cc/tag/301/" target="_blank" class="keywords">301</a>_1595@)</span>:</span>
<span class="hljs-comment" style="color: #7285b7;"># 执行<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>前做点事</span>
result = self.func(*args,**@R_<a href="https://www.jb51.cc/tag/301/" target="_blank" class="keywords">301</a>_1595@)
<span class="hljs-comment" style="color: #7285b7;"># 执行<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>后干点啥</span>
<span class="hljs-keyword" style="color: #ebbbff;">return</span> result
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def</span> <span class="hljs-title" style="color: #7285b7;">__call__</span><span class="hljs-params" style="color: #ffc58f;">(self,*args,**@R_<a href="https://www.jb51.cc/tag/301/" target="_blank" class="keywords">301</a>_1595@)</span>:</span>
<span class="hljs-comment" style="color: #7285b7;"># 执行<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>前做点事</span>
result = self.func(*args,**@R_<a href="https://www.jb51.cc/tag/301/" target="_blank" class="keywords">301</a>_1595@)
<span class="hljs-comment" style="color: #7285b7;"># 执行<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>后干点啥</span>
<span class="hljs-keyword" style="color: #ebbbff;">return</span> result
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def</span> <span class="hljs-title" style="color: #7285b7;">__call__</span><span class="hljs-params" style="color: #ffc58f;">(self,*args,**@R_<a href="https://www.jb51.cc/tag/301/" target="_blank" class="keywords">301</a>_1595@)</span>:</span>
<span class="hljs-comment" style="color: #7285b7;"># 执行<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>前做点事</span>
result = self.func(*args,**@R_<a href="https://www.jb51.cc/tag/301/" target="_blank" class="keywords">301</a>_1595@)
<span class="hljs-comment" style="color: #7285b7;"># 执行<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>后干点啥</span>
<span class="hljs-keyword" style="color: #ebbbff;">return</span> result
):
:
301_1595@):
函数前做点事
print(what_i_say)
result = func(*args,**@R_301_1595@)
函数后干点啥
result
inner
actual_decorator
<span class="hljs-decorator">@decorator('我要做一次加法')
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">add<span class="hljs-params" style="color: #ffc58f;">(a,b):
<span class="hljs-keyword" style="color: #ebbbff;">return a + b
print(add(<span class="hljs-number" style="color: #ffc58f;">3,<span class="hljs-number" style="color: #ffc58f;">9))
<span class="hljs-decorator">@decorator('我要做一次加法')
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">add<span class="hljs-params" style="color: #ffc58f;">(a,b):
<span class="hljs-keyword" style="color: #ebbbff;">return a + b
print(add(<span class="hljs-number" style="color: #ffc58f;">3,<span class="hljs-number" style="color: #ffc58f;">9))
<span class="hljs-decorator">@decorator('我要做一次加法')
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">add<span class="hljs-params" style="color: #ffc58f;">(a,b):
<span class="hljs-keyword" style="color: #ebbbff;">return a + b
print(add(<span class="hljs-number" style="color: #ffc58f;">3,<span class="hljs-number" style="color: #ffc58f;">9))
.actual_decorator.
.inner at 0x00000211E632CB70>
函数,所以它把装饰器里的那个给打印出来了。可是把这个玩意打印出来干嘛呀,我要原来的函数名,有没有什么办法啊,办法当然有,如下
functools wraps
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">decorator<span class="hljs-params" style="color: #ffc58f;">(what_i_say):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">actual_decorator<span class="hljs-params" style="color: #ffc58f;">(func):
<span class="hljs-decorator">@wraps(func)</span>
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def</span> <span class="hljs-title" style="color: #7285b7;">inner</span><span class="hljs-params" style="color: #ffc58f;">(*args,**@R_<a href="https://www.jb51.cc/tag/301/" target="_blank" class="keywords">301</a>_1595@)
<span class="hljs-comment" style="color: #7285b7;"># 执行<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>后干点啥</span>
<span class="hljs-keyword" style="color: #ebbbff;">return</span> result
<span class="hljs-keyword" style="color: #ebbbff;">return</span> inner
<span class="hljs-keyword" style="color: #ebbbff;">return</span> actual_decorator
<span class="hljs-decorator">@wraps(func)</span>
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def</span> <span class="hljs-title" style="color: #7285b7;">inner</span><span class="hljs-params" style="color: #ffc58f;">(*args,**@R_<a href="https://www.jb51.cc/tag/301/" target="_blank" class="keywords">301</a>_1595@)
<span class="hljs-comment" style="color: #7285b7;"># 执行<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>后干点啥</span>
<span class="hljs-keyword" style="color: #ebbbff;">return</span> result
<span class="hljs-keyword" style="color: #ebbbff;">return</span> inner
<span class="hljs-keyword" style="color: #ebbbff;">return</span> actual_decorator
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">decorator<span class="hljs-params" style="color: #ffc58f;">(what_i_say):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">actual_decorator<span class="hljs-params" style="color: #ffc58f;">(func):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">decorator<span class="hljs-params" style="color: #ffc58f;">(what_i_say):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">actual_decorator<span class="hljs-params" style="color: #ffc58f;">(func):
<span class="hljs-decorator">@wraps(func)</span>
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def</span> <span class="hljs-title" style="color: #7285b7;">inner</span><span class="hljs-params" style="color: #ffc58f;">(*args,**@R_<a href="https://www.jb51.cc/tag/301/" target="_blank" class="keywords">301</a>_1595@)
<span class="hljs-comment" style="color: #7285b7;"># 执行<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>后干点啥</span>
<span class="hljs-keyword" style="color: #ebbbff;">return</span> result
<span class="hljs-keyword" style="color: #ebbbff;">return</span> inner
<span class="hljs-keyword" style="color: #ebbbff;">return</span> actual_decorator
要做一次加法')
:
a + b
print(add)
<span class="hljs-comment" style="color: #7285b7;"># <function add at 0x000001F5DEE966A8>
print(add)
<span class="hljs-comment" style="color: #7285b7;"># <function add at 0x000001F5DEE966A8>
print(add)
<span class="hljs-comment" style="color: #7285b7;"># <function add at 0x000001F5DEE966A8>
如果您也喜欢它,动动您的小指点个赞吧