{"id":1100855,"date":"2025-01-08T15:43:51","date_gmt":"2025-01-08T07:43:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1100855.html"},"modified":"2025-01-08T15:43:54","modified_gmt":"2025-01-08T07:43:54","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e7%94%a8%e7%b1%bb%e6%9e%84%e6%88%90%e8%a3%85%e9%a5%b0%e5%99%a8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1100855.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u7528\u7c7b\u6784\u6210\u88c5\u9970\u5668"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063922\/5b4fc1e7-31ea-4e5a-98d0-f4455786ad80.webp\" alt=\"python\u4e2d\u5982\u4f55\u7528\u7c7b\u6784\u6210\u88c5\u9970\u5668\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u6765\u6784\u5efa\u88c5\u9970\u5668\u3002<strong>\u4f7f\u7528\u7c7b\u6784\u5efa\u88c5\u9970\u5668\u7684\u65b9\u5f0f\u5305\u62ec\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u5e76\u5728\u7c7b\u4e2d\u5b9e\u73b0 <code>__call__<\/code> \u65b9\u6cd5\u3001\u5728\u88c5\u9970\u5668\u7c7b\u4e2d\u4f20\u9012\u88ab\u88c5\u9970\u51fd\u6570\u3001\u5229\u7528\u5b9e\u4f8b\u5c5e\u6027\u4fdd\u5b58\u72b6\u6001<\/strong>\u3002\u4e0b\u9762\u8be6\u7ec6\u5c55\u5f00\u5176\u4e2d\u4e00\u70b9\uff1a\u901a\u8fc7 <code>__call__<\/code> \u65b9\u6cd5\u4f7f\u7c7b\u7684\u5b9e\u4f8b\u5177\u5907\u53ef\u8c03\u7528\u6027\uff0c\u4ece\u800c\u5b9e\u73b0\u88c5\u9970\u5668\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u88c5\u9970\u5668\u7c7b<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u88c5\u9970\u5668\u7c7b\uff0c\u5e76\u5728\u8fd9\u4e2a\u7c7b\u4e2d\u5b9e\u73b0 <code>__call__<\/code> \u65b9\u6cd5\u3002<code>__call__<\/code> \u65b9\u6cd5\u4f7f\u5f97\u7c7b\u7684\u5b9e\u4f8b\u53ef\u4ee5\u50cf\u51fd\u6570\u4e00\u6837\u88ab\u8c03\u7528\uff0c\u4ece\u800c\u5b9e\u73b0\u88c5\u9970\u5668\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyDecorator:<\/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(&quot;Before the function call&quot;)<\/p>\n<p>        result = self.func(*args, kwargs)<\/p>\n<p>        print(&quot;After the function call&quot;)<\/p>\n<p>        return result<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7c7b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7 <code>__init__<\/code> \u65b9\u6cd5\u63a5\u6536\u88ab\u88c5\u9970\u7684\u51fd\u6570\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u4e3a\u5b9e\u4f8b\u5c5e\u6027 <code>self.func<\/code>\u3002\u5728 <code>__call__<\/code> \u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5728\u8c03\u7528\u88ab\u88c5\u9970\u51fd\u6570\u4e4b\u524d\u548c\u4e4b\u540e\u6253\u5370\u4e00\u4e9b\u4fe1\u606f\uff0c\u5e76\u8fd4\u56de\u51fd\u6570\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u88c5\u9970\u5668\u7c7b<\/h3>\n<\/p>\n<p><p>\u5b9a\u4e49\u597d\u88c5\u9970\u5668\u7c7b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u4f7f\u7528\u51fd\u6570\u88c5\u9970\u5668\u4e00\u6837\u4f7f\u7528\u8fd9\u4e2a\u7c7b\u88c5\u9970\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@MyDecorator<\/p>\n<p>def say_hello(name):<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<p>say_hello(&quot;World&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>say_hello<\/code> \u51fd\u6570\u88ab <code>MyDecorator<\/code> \u7c7b\u88c5\u9970\uff0c\u8c03\u7528 <code>say_hello<\/code> \u51fd\u6570\u65f6\uff0c\u5b9e\u9645\u4e0a\u662f\u8c03\u7528 <code>MyDecorator<\/code> \u7684\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u88c5\u9970\u5668\u7c7b\u7684\u72b6\u6001\u4fdd\u5b58<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u5e0c\u671b\u88c5\u9970\u5668\u80fd\u591f\u4fdd\u5b58\u4e00\u4e9b\u72b6\u6001\u4fe1\u606f\uff0c\u4f8b\u5982\u7edf\u8ba1\u51fd\u6570\u88ab\u8c03\u7528\u7684\u6b21\u6570\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u7c7b\u7684\u5b9e\u4f8b\u5c5e\u6027\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class CountCalls:<\/p>\n<p>    def __init__(self, func):<\/p>\n<p>        self.func = func<\/p>\n<p>        self.num_calls = 0<\/p>\n<p>    def __call__(self, *args, kwargs):<\/p>\n<p>        self.num_calls += 1<\/p>\n<p>        print(f&quot;Call {self.num_calls} of {self.func.__name__}&quot;)<\/p>\n<p>        return self.func(*args, kwargs)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>CountCalls<\/code> \u88c5\u9970\u5668\u7c7b\u901a\u8fc7 <code>self.num_calls<\/code> \u5c5e\u6027\u8bb0\u5f55\u51fd\u6570\u88ab\u8c03\u7528\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u72b6\u6001\u4fdd\u5b58\u7684\u88c5\u9970\u5668\u7c7b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">@CountCalls<\/p>\n<p>def say_hello(name):<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<p>say_hello(&quot;Alice&quot;)<\/p>\n<p>say_hello(&quot;Bob&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6bcf\u6b21\u8c03\u7528 <code>say_hello<\/code> \u51fd\u6570\u65f6\uff0c<code>CountCalls<\/code> \u88c5\u9970\u5668\u7c7b\u90fd\u4f1a\u8bb0\u5f55\u8c03\u7528\u6b21\u6570\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u88c5\u9970\u5668\u7c7b\u7684\u66f4\u591a\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u7c7b\u88c5\u9970\u5668\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u7b80\u5355\u7684\u51fd\u6570\u88c5\u9970\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u573a\u666f\uff0c\u4f8b\u5982\u88c5\u9970\u7c7b\u65b9\u6cd5\u6216\u9759\u6001\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u88c5\u9970\u7c7b\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    @CountCalls<\/p>\n<p>    def method(self):<\/p>\n<p>        print(&quot;Method called&quot;)<\/p>\n<p>obj = MyClass()<\/p>\n<p>obj.method()<\/p>\n<p>obj.method()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u88c5\u9970\u9759\u6001\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    @staticmethod<\/p>\n<p>    @CountCalls<\/p>\n<p>    def static_method():<\/p>\n<p>        print(&quot;Static method called&quot;)<\/p>\n<p>MyClass.static_method()<\/p>\n<p>MyClass.static_method()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u4f7f\u7528\u7c7b\u6784\u5efa\u88c5\u9970\u5668\u7684\u8fc7\u7a0b\u5305\u62ec\u5b9a\u4e49\u4e00\u4e2a\u88c5\u9970\u5668\u7c7b\u3001\u5b9e\u73b0 <code>__call__<\/code> \u65b9\u6cd5\u3001\u5728\u88c5\u9970\u5668\u7c7b\u4e2d\u4f20\u9012\u88ab\u88c5\u9970\u51fd\u6570\u3001\u5229\u7528\u5b9e\u4f8b\u5c5e\u6027\u4fdd\u5b58\u72b6\u6001\u3002\u8fd9\u6837\u4e0d\u4ec5\u53ef\u4ee5\u5b9e\u73b0\u88c5\u9970\u51fd\u6570\u7684\u529f\u80fd\uff0c\u8fd8\u53ef\u4ee5\u5728\u88c5\u9970\u5668\u4e2d\u4fdd\u5b58\u72b6\u6001\u4fe1\u606f\uff0c\u4f7f\u88c5\u9970\u5668\u66f4\u52a0\u7075\u6d3b\u548c\u5f3a\u5927\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u88c5\u9970\u5668\u5728Python\u4e2d\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f<\/strong><br \/>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u5141\u8bb8\u7528\u6237\u5728\u4e0d\u4fee\u6539\u51fd\u6570\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u589e\u52a0\u989d\u5916\u529f\u80fd\u7684\u65b9\u5f0f\u3002\u5b83\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u8fd4\u56de\u51fd\u6570\u7684\u51fd\u6570\u3002\u5728Python\u4e2d\uff0c\u88c5\u9970\u5668\u53ef\u4ee5\u7528\u51fd\u6570\u6216\u7c7b\u6765\u5b9e\u73b0\u3002\u4f7f\u7528\u7c7b\u6784\u6210\u7684\u88c5\u9970\u5668\u901a\u5e38\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u72b6\u6001\u7ba1\u7406\u548c\u989d\u5916\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>\u4f7f\u7528\u7c7b\u4f5c\u4e3a\u88c5\u9970\u5668\u65f6\uff0c\u6709\u54ea\u4e9b\u4f18\u52bf\uff1f<\/strong><br \/>\u5229\u7528\u7c7b\u6784\u6210\u88c5\u9970\u5668\u7684\u4e3b\u8981\u4f18\u52bf\u5728\u4e8e\u80fd\u591f\u5229\u7528\u7c7b\u7684\u5b9e\u4f8b\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u8fd9\u4f7f\u5f97\u72b6\u6001\u7ba1\u7406\u548c\u529f\u80fd\u6269\u5c55\u53d8\u5f97\u66f4\u52a0\u7075\u6d3b\u548c\u5f3a\u5927\u3002\u7c7b\u88c5\u9970\u5668\u53ef\u4ee5\u4fdd\u5b58\u72b6\u6001\uff0c\u5141\u8bb8\u5728\u88c5\u9970\u5668\u5185\u90e8\u7ef4\u62a4\u6570\u636e\uff0c\u6216\u5728\u591a\u4e2a\u51fd\u6570\u8c03\u7528\u4e4b\u95f4\u5171\u4eab\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u7c7b\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u88c5\u9970\u5668\u793a\u4f8b\uff1f<\/strong><br \/>\u8981\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u7c7b\u88c5\u9970\u5668\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u5e76\u5728\u8be5\u7c7b\u4e2d\u5b9e\u73b0<code>__call__<\/code>\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f7f\u5f97\u7c7b\u7684\u5b9e\u4f8b\u53ef\u4ee5\u50cf\u51fd\u6570\u4e00\u6837\u88ab\u8c03\u7528\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7c7b\u88c5\u9970\u5668\uff0c\u7528\u4e8e\u8bb0\u5f55\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\uff0c\u4ee3\u7801\u7ed3\u6784\u901a\u5e38\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">class Timer:\n    def __call__(self, func):\n        import time\n        def wrapper(*args, **kwargs):\n            start_time = time.time()\n            result = func(*args, **kwargs)\n            end_time = time.time()\n            print(f&quot;Function {func.__name__} took {end_time - start_time:.4f} seconds&quot;)\n            return result\n        return wrapper\n\n@Timer()\ndef my_function():\n    # \u51fd\u6570\u4f53\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e2a\u793a\u4f8b\uff0c\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u4f7f\u7528\u7c7b\u88c5\u9970\u5668\u6765\u589e\u5f3a\u51fd\u6570\u7684\u529f\u80fd\uff0c\u540c\u65f6\u4fdd\u6301\u4ee3\u7801\u7684\u6574\u6d01\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u6765\u6784\u5efa\u88c5\u9970\u5668\u3002\u4f7f\u7528\u7c7b\u6784\u5efa\u88c5\u9970\u5668\u7684\u65b9\u5f0f\u5305\u62ec\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u5e76\u5728\u7c7b\u4e2d\u5b9e\u73b0 __call__ [&hellip;]","protected":false},"author":3,"featured_media":1100863,"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\/1100855"}],"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=1100855"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100855\/revisions"}],"predecessor-version":[{"id":1100868,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100855\/revisions\/1100868"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1100863"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1100855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1100855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1100855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}