{"id":173653,"date":"2024-05-08T18:16:05","date_gmt":"2024-05-08T10:16:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/173653.html"},"modified":"2024-05-08T18:16:09","modified_gmt":"2024-05-08T10:16:09","slug":"%e5%a6%82%e4%bd%95%e7%94%a8%e5%a5%bdpython%e7%9a%84%e4%bf%ae%e9%a5%b0%e5%99%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/173653.html","title":{"rendered":"\u5982\u4f55\u7528\u597dpython\u7684\u4fee\u9970\u5668"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27045617\/d5298072-7495-43ac-ad82-e83a800f12b8.webp\" alt=\"\u5982\u4f55\u7528\u597dpython\u7684\u4fee\u9970\u5668\" \/><\/p>\n<p><p>\u4fee\u9970\u5668\u5728Python\u4e2d\u662f\u4e00\u79cd\u9ad8\u7ea7\u529f\u80fd\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u5728\u4e0d\u4fee\u6539\u539f\u59cb\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u6269\u5c55\u6216\u4fee\u6539\u51fd\u6570\u7684\u884c\u4e3a\u3002<strong>\u5728Python\u4e2d\u9ad8\u6548\u4f7f\u7528\u4fee\u9970\u5668\u7684\u5173\u952e\u662f\u7406\u89e3\u5b83\u7684\u5de5\u4f5c\u673a\u5236\u3001\u719f\u6089\u5e38\u7528\u4fee\u9970\u5668\u6a21\u5f0f\u3001\u5408\u7406\u8bbe\u8ba1\u81ea\u5b9a\u4e49\u4fee\u9970\u5668\u3001\u5e76\u59a5\u5584\u5904\u7406\u4fee\u9970\u5668\u5806\u53e0\u548c\u53c2\u6570\u5316\u95ee\u9898\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u60f3\u6df1\u5165\u7406\u89e3\u4fee\u9970\u5668\uff0c\u91cd\u8981\u7684\u662f\u638c\u63e1\u5b83\u7684\u5b9a\u4e49\u548c\u8c03\u7528\u8fc7\u7a0b\u3002\u4fee\u9970\u5668\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u63a5\u6536\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u51fd\u6570\u3002\u5728\u6b64\u8fc7\u7a0b\u4e2d\uff0c\u4fee\u9970\u5668\u900f\u660e\u5730\u201c\u5305\u88f9\u201d\u4e86\u8f93\u5165\u7684\u51fd\u6570\uff0c\u5e76\u53ef\u4ee5\u5728\u4e0d\u6539\u53d8\u5176\u884c\u4e3a\u7684\u524d\u63d0\u4e0b\uff0c\u589e\u52a0\u989d\u5916\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7406\u89e3\u4fee\u9970\u5668\u7684\u5de5\u4f5c\u539f\u7406<\/h3>\n<\/p>\n<p><p>\u4fee\u9970\u5668\u7684\u6838\u5fc3\u6982\u5ff5\u662f\u5c06\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u8f93\u5165\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u6709\u7740\u989d\u5916\u529f\u80fd\u7684\u65b0\u51fd\u6570\u3002\u6700\u7b80\u5355\u7684\u4fee\u9970\u5668\u4f8b\u5b50\u5982\u4e0b\u6240\u793a:<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def simple_decorator(func):<\/p>\n<p>    def wrapper():<\/p>\n<p>        print(&quot;Something is happening before the function is called.&quot;)<\/p>\n<p>        func()<\/p>\n<p>        print(&quot;Something is happening after the function is called.&quot;)<\/p>\n<p>    return wrapper<\/p>\n<p>@simple_decorator<\/p>\n<p>def say_hello():<\/p>\n<p>    print(&quot;Hello!&quot;)<\/p>\n<p>say_hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>simple_decorator<\/code> \u63a5\u53d7\u4e86\u4e00\u4e2a\u51fd\u6570 <code>say_hello<\/code> \u5e76\u8fd4\u56de\u4e86\u4e00\u4e2a\u65b0\u7684\u51fd\u6570 <code>wrapper<\/code>\u3002\u901a\u8fc7\u5728\u51fd\u6570\u4e0a\u65b9\u4f7f\u7528 <code>@simple_decorator<\/code> \u8bed\u6cd5\uff0c\u6211\u4eec\u5b9e\u9645\u4e0a\u662f\u6267\u884c\u4e86 <code>say_hello = simple_decorator(say_hello)<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u719f\u6089\u5e38\u7528\u4fee\u9970\u5668\u6a21\u5f0f<\/h3>\n<\/p>\n<p><h4>1. \u65e5\u5fd7\u8bb0\u5f55\u4fee\u9970\u5668<\/h4>\n<\/p>\n<p><p>\u5e38\u89c1\u7684\u4fee\u9970\u5668\u7528\u6cd5\u4e4b\u4e00\u662f\u7528\u4e8e\u65e5\u5fd7\u8bb0\u5f55\uff0c\u76ee\u7684\u5728\u4e8e\u5728\u51fd\u6570\u6267\u884c\u524d\u540e\u6253\u5370\u76f8\u5e94\u7684\u65e5\u5fd7\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import functools<\/p>\n<p>def logging_decorator(func):<\/p>\n<p>    @functools.wraps(func)<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        print(f&quot;Logging: function {func.__name__} is about to run&quot;)<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(f&quot;Logging: function {func.__name__} has finished running&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u4fee\u9970\u5668\u5229\u7528 <code>functools.wraps<\/code> \u6765\u4fdd\u7559\u539f\u59cb\u51fd\u6570\u7684\u5143\u6570\u636e\uff0c\u5982\u5176\u540d\u79f0\u548c\u6587\u6863\u5b57\u7b26\u4e32\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2. \u6027\u80fd\u8ba1\u91cf\u4fee\u9970\u5668<\/h4>\n<\/p>\n<p><p>\u7528\u4e8e\u6d4b\u8bd5\u4ee3\u7801\u6267\u884c\u65f6\u95f4\u7684\u4fee\u9970\u5668\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u91cf\u5316\u51fd\u6570\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def timing_decorator(func):<\/p>\n<p>    @functools.wraps(func)<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        start_time = time.perf_counter()<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        end_time = time.perf_counter()<\/p>\n<p>        print(f&quot;Function {func.__name__} took {end_time - start_time} seconds to run.&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8bbe\u8ba1\u81ea\u5b9a\u4e49\u4fee\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u5f53\u521b\u5efa\u81ea\u5b9a\u4e49\u4fee\u9970\u5668\u65f6\uff0c\u5e94\u5f53\u8003\u8651\u6e05\u6670\u7684\u547d\u540d\u3001\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u4ee5\u53ca\u8db3\u591f\u7684\u7075\u6d3b\u6027\u6765\u9002\u5e94\u4e0d\u540c\u60c5\u5883\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u5e26\u6709\u53c2\u6570\u7684\u590d\u6742\u4fee\u9970\u5668<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u53c2\u6570\u6765\u8c03\u6574\u4fee\u9970\u5668\u7684\u884c\u4e3a\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5e26\u53c2\u6570\u7684\u4fee\u9970\u5668\u6a21\u677f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def parametrized_decorator(parameter):<\/p>\n<p>    def decorator(func):<\/p>\n<p>        @functools.wraps(func)<\/p>\n<p>        def wrapper(*args, kwargs):<\/p>\n<p>            # Do something with the parameter here<\/p>\n<p>            result = func(*args, kwargs)<\/p>\n<p>            # Do something with the parameter here<\/p>\n<p>            return result<\/p>\n<p>        return wrapper<\/p>\n<p>    return decorator<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u81ea\u5b9a\u4e49\u7c7b\u4fee\u9970\u5668<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u51fd\u6570\u5b9a\u4e49\u4fee\u9970\u5668\u5916\uff0c\u7c7b\u4e5f\u53ef\u4ee5\u88ab\u7528\u6765\u5b9e\u73b0\u4fee\u9970\u5668\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class DecoratorClass:<\/p>\n<p>    def __init__(self, func):<\/p>\n<p>        self.func = func<\/p>\n<p>    def __call__(self, *args, kwargs):<\/p>\n<p>        print(&#039;Class decorator running&#039;)<\/p>\n<p>        return self.func(*args, kwargs)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u59a5\u5584\u5904\u7406\u4fee\u9970\u5668\u5806\u53e0\u548c\u53c2\u6570\u5316\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u4fee\u9970\u5668\u5806\u53e0\u662f\u4e00\u4e2a\u5e38\u89c1\u6a21\u5f0f\uff0c\u5b83\u6d89\u53ca\u5728\u4e00\u4e2a\u51fd\u6570\u4e0a\u53e0\u52a0\u591a\u4e2a\u4fee\u9970\u5668\u3002\u5904\u7406\u4fee\u9970\u5668\u5806\u53e0\u65f6\u8981\u786e\u4fdd\u4fee\u9970\u5668\u7684\u6267\u884c\u987a\u5e8f\u548c\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@logging_decorator<\/p>\n<p>@timing_decorator<\/p>\n<p>def some_function(params):<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>timing_decorator<\/code> \u5c06\u9996\u5148\u88ab\u5e94\u7528\u5230 <code>some_function<\/code> \u4e0a\uff0c\u7d27\u63a5\u7740\u662f <code>logging_decorator<\/code>\u3002\u7406\u89e3\u8fd9\u4e00\u70b9\u5bf9\u4e8e\u9884\u6d4b\u4fee\u9970\u5668\u7ec4\u5408\u7684\u884c\u4e3a\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u7406\u89e3\u548c\u638c\u63e1\u597dPython\u7684\u4fee\u9970\u5668\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u3001\u964d\u4f4e\u590d\u6742\u5ea6\uff0c\u5e76\u4e14\u53ef\u4ee5\u8ba9\u4ee3\u7801\u770b\u8d77\u6765\u66f4\u52a0\u7b80\u6d01\u3001\u4f18\u96c5\u3002<strong>\u638c\u63e1\u4fee\u9970\u5668\u7684\u5b9a\u4e49\u3001\u4f7f\u7528\u5e38\u89c1\u6a21\u5f0f\u3001\u8bbe\u8ba1\u81ea\u5b9a\u4e49\u4fee\u9970\u5668\u3001\u4ee5\u53ca\u5904\u7406\u5806\u53e0\u548c\u53c2\u6570\u5316\u95ee\u9898<\/strong>\u662f\u4f7f\u7528\u4fee\u9970\u5668\u65f6\u9700\u8981\u6ce8\u610f\u7684\u56db\u4e2a\u5173\u952e\u70b9\u3002\u901a\u8fc7\u5bf9\u8fd9\u4e9b\u8981\u70b9\u7684\u719f\u7ec3\u8fd0\u7528\uff0c\u4efb\u4f55Python\u5f00\u53d1\u8005\u90fd\u80fd\u591f\u5728\u4e0d\u7834\u574f\u5df2\u6709\u4ee3\u7801\u7ed3\u6784\u7684\u524d\u63d0\u4e0b\uff0c\u5b9e\u73b0\u529f\u80fd\u7684\u6269\u5c55\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q1: Python\u4e2d\u7684\u4fee\u9970\u5668\u6709\u4ec0\u4e48\u4f5c\u7528\uff1f<\/strong><br \/>\n\u4fee\u9970\u5668\u5728Python\u4e2d\u8d77\u5230\u4e86\u5f88\u91cd\u8981\u7684\u4f5c\u7528\uff0c\u5b83\u4eec\u5141\u8bb8\u6211\u4eec\u5728\u4e0d\u6539\u53d8\u539f\u6709\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u5bf9\u51fd\u6570\u8fdb\u884c\u6269\u5c55\u548c\u4fee\u6539\u3002\u901a\u8fc7\u4fee\u9970\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u51fd\u6570\u6267\u884c\u524d\u540e\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u65e5\u5fd7\u8bb0\u5f55\u3001\u6027\u80fd\u7edf\u8ba1\u6216\u8005\u6743\u9650\u6821\u9a8c\u7b49\u3002<\/p>\n<p><strong>Q2: \u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u4fee\u9970\u5668\uff1f<\/strong><br \/>\n\u8981\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u4fee\u9970\u5668\uff0c\u9996\u5148\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u88c5\u9970\u51fd\u6570\u3002\u8fd9\u4e2a\u88c5\u9970\u51fd\u6570\u5e94\u8be5\u63a5\u6536\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u3002\u5728\u65b0\u7684\u51fd\u6570\u5185\u90e8\uff0c\u53ef\u4ee5\u6dfb\u52a0\u4f60\u60f3\u8981\u7684\u989d\u5916\u529f\u80fd\uff0c\u7136\u540e\u8c03\u7528\u539f\u59cb\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<p><strong>Q3: \u6709\u6ca1\u6709\u4e00\u4e9b\u5e38\u7528\u7684Python\u4fee\u9970\u5668\u6848\u4f8b\uff1f<\/strong><br \/>\n\u662f\u7684\uff0c\u6709\u5f88\u591a\u5e38\u7528\u7684Python\u4fee\u9970\u5668\u3002\u4f8b\u5982\uff0c@staticmethod\u4fee\u9970\u5668\u7528\u4e8e\u5c06\u51fd\u6570\u8f6c\u5316\u4e3a\u9759\u6001\u65b9\u6cd5\uff1b@classmethod\u4fee\u9970\u5668\u7528\u4e8e\u5c06\u51fd\u6570\u8f6c\u5316\u4e3a\u7c7b\u65b9\u6cd5\uff1b@property\u4fee\u9970\u5668\u7528\u4e8e\u5c06\u65b9\u6cd5\u8f6c\u5316\u4e3a\u5c5e\u6027\u8bbf\u95ee\uff1b@login_required\u4fee\u9970\u5668\u7528\u4e8e\u68c0\u67e5\u7528\u6237\u662f\u5426\u767b\u5f55\u7b49\u3002\u8fd9\u4e9b\u4fee\u9970\u5668\u90fd\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7b80\u5316\u4ee3\u7801\uff0c\u5e76\u63d0\u9ad8\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4fee\u9970\u5668\u5728Python\u4e2d\u662f\u4e00\u79cd\u9ad8\u7ea7\u529f\u80fd\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u5728\u4e0d\u4fee\u6539\u539f\u59cb\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u6269\u5c55\u6216\u4fee\u6539\u51fd\u6570\u7684\u884c\u4e3a\u3002\u5728Python [&hellip;]","protected":false},"author":3,"featured_media":173657,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/173653"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=173653"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/173653\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/173657"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=173653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=173653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=173653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}