您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python装饰器的实现

5b51 2022/1/14 8:24:16 python 字数 18852 阅读 605 来源 www.jb51.cc/python

说起装饰器我们可能已经很熟悉了(不了解的可以查看python基础学习——装饰器),随手就可以写一个简单的装饰器 def decorator(func): def inner(*args, **kwar

概述

<div class="markdown-here-wrapper" data-md-url="https://i.cnblogs.com/EditPosts.aspx?opt=1"&gt;
<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"&gt;python基础学习——装饰器),随手就可以写一个简单的装饰器

 :
     301_1595@):
        函数前做点事
        result = func(*args,**@R_301_1595@)
        函数后干点啥
         result
     inner

函数的方式实现的,但我们也可以用类的方式写一个同样的装饰器,还记得方法

 :
     __init__:
        self.func = func
<span class="hljs-function" style="color: #bbdaff;"&gt;<span class="hljs-keyword" style="color: #ebbbff;"&gt;def</span> <span class="hljs-title" style="color: #7285b7;"&gt;__call__</span><span class="hljs-params" style="color: #ffc58f;"&gt;(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;"&gt;# 执行<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;"&gt;# 执行<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>后干点啥</span>
    <span class="hljs-keyword" style="color: #ebbbff;"&gt;return</span> result

<span class="hljs-function" style="color: #bbdaff;"&gt;<span class="hljs-keyword" style="color: #ebbbff;"&gt;def</span> <span class="hljs-title" style="color: #7285b7;"&gt;__call__</span><span class="hljs-params" style="color: #ffc58f;"&gt;(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;"&gt;# 执行<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;"&gt;# 执行<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>后干点啥</span>
    <span class="hljs-keyword" style="color: #ebbbff;"&gt;return</span> result

<span class="hljs-function" style="color: #bbdaff;"&gt;<span class="hljs-keyword" style="color: #ebbbff;"&gt;def</span> <span class="hljs-title" style="color: #7285b7;"&gt;__call__</span><span class="hljs-params" style="color: #ffc58f;"&gt;(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;"&gt;# 执行<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;"&gt;# 执行<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>后干点啥</span>
    <span class="hljs-keyword" style="color: #ebbbff;"&gt;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"&gt;@wraps(func)</span>
    <span class="hljs-function" style="color: #bbdaff;"&gt;<span class="hljs-keyword" style="color: #ebbbff;"&gt;def</span> <span class="hljs-title" style="color: #7285b7;"&gt;inner</span><span class="hljs-params" style="color: #ffc58f;"&gt;(*args,**@R_<a href="https://www.jb51.cc/tag/301/" target="_blank" class="keywords">301</a>_1595@)
        <span class="hljs-comment" style="color: #7285b7;"&gt;# 执行<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>后干点啥</span>
        <span class="hljs-keyword" style="color: #ebbbff;"&gt;return</span> result
    <span class="hljs-keyword" style="color: #ebbbff;"&gt;return</span> inner
<span class="hljs-keyword" style="color: #ebbbff;"&gt;return</span> actual_decorator

    <span class="hljs-decorator"&gt;@wraps(func)</span>
    <span class="hljs-function" style="color: #bbdaff;"&gt;<span class="hljs-keyword" style="color: #ebbbff;"&gt;def</span> <span class="hljs-title" style="color: #7285b7;"&gt;inner</span><span class="hljs-params" style="color: #ffc58f;"&gt;(*args,**@R_<a href="https://www.jb51.cc/tag/301/" target="_blank" class="keywords">301</a>_1595@)
        <span class="hljs-comment" style="color: #7285b7;"&gt;# 执行<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>后干点啥</span>
        <span class="hljs-keyword" style="color: #ebbbff;"&gt;return</span> result
    <span class="hljs-keyword" style="color: #ebbbff;"&gt;return</span> inner
<span class="hljs-keyword" style="color: #ebbbff;"&gt;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"&gt;@wraps(func)</span>
    <span class="hljs-function" style="color: #bbdaff;"&gt;<span class="hljs-keyword" style="color: #ebbbff;"&gt;def</span> <span class="hljs-title" style="color: #7285b7;"&gt;inner</span><span class="hljs-params" style="color: #ffc58f;"&gt;(*args,**@R_<a href="https://www.jb51.cc/tag/301/" target="_blank" class="keywords">301</a>_1595@)
        <span class="hljs-comment" style="color: #7285b7;"&gt;# 执行<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>后干点啥</span>
        <span class="hljs-keyword" style="color: #ebbbff;"&gt;return</span> result
    <span class="hljs-keyword" style="color: #ebbbff;"&gt;return</span> inner
<span class="hljs-keyword" style="color: #ebbbff;"&gt;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>


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶