{"id":176145,"date":"2024-05-08T19:04:47","date_gmt":"2024-05-08T11:04:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/176145.html"},"modified":"2024-05-08T19:04:52","modified_gmt":"2024-05-08T11:04:52","slug":"python%e7%9a%84%e7%b1%bb%e6%96%b9%e6%b3%95%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/176145.html","title":{"rendered":"Python\u7684\u7c7b\u65b9\u6cd5\u600e\u4e48\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27055710\/65e6c0f6-d80f-453c-be8b-0d3a0269dac5.webp\" alt=\"Python\u7684\u7c7b\u65b9\u6cd5\u600e\u4e48\u4f7f\u7528\" \/><\/p>\n<p><p>\u7c7b\u65b9\u6cd5\u5728Python\u4e2d\u662f\u4e00\u79cd\u4f9d\u9644\u4e8e\u7c7b\u800c\u975e\u7c7b\u7684\u5b9e\u4f8b\u7684\u65b9\u6cd5\u3002\u5b83\u4eec\u7684\u6700\u5927\u7279\u70b9\u662f\uff0c<strong>\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u7c7b\u672c\u8eab\uff0c\u901a\u5e38\u7528cls\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u540d\u79f0\u3001\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7c7b\u8c03\u7528\u800c\u4e0d\u662f\u901a\u8fc7\u7c7b\u7684\u5b9e\u4f8b\u3001\u5e38\u7528\u4e8e\u5b9a\u4e49\u5de5\u5382\u65b9\u6cd5<\/strong>\u3002\u7c7b\u65b9\u6cd5\u4f7f\u7528<code>@classmethod<\/code>\u88c5\u9970\u5668\u6765\u6807\u8bc6\u3002\u4ee5\u5b9a\u4e49\u5de5\u5382\u65b9\u6cd5\u4e3a\u4f8b\uff0c\u8fd9\u662f\u7c7b\u65b9\u6cd5\u7684\u4e00\u4e2a\u5178\u578b\u5e94\u7528\u3002\u5de5\u5382\u65b9\u6cd5\u5229\u7528\u7c7b\u65b9\u6cd5\u7684\u7279\u6027\uff0c\u5141\u8bb8\u5728\u4e00\u4e2a\u7c7b\u7684\u5185\u90e8\u5b9a\u4e49\u591a\u4e2a\u6784\u9020\u5668\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u7c7b\u65b9\u6cd5\u7684\u5b9a\u4e49\u548c\u4f7f\u7528<\/h2>\n<\/p>\n<p><p>\u7c7b\u65b9\u6cd5\u901a\u8fc7\u5728\u65b9\u6cd5\u524d\u4f7f\u7528<code>@classmethod<\/code>\u88c5\u9970\u5668\u6765\u5b9a\u4e49\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u7c7b\u672c\u8eab\uff0c\u800c\u4e0d\u662f\u7c7b\u7684\u5b9e\u4f8b\u3002\u8fd9\u610f\u5473\u7740\u7c7b\u65b9\u6cd5\u53ef\u4ee5\u5728\u4e0d\u521b\u5efa\u7c7b\u5b9e\u4f8b\u7684\u60c5\u51b5\u4e0b\u88ab\u8c03\u7528\u3002\u8fd9\u5728\u9700\u8981\u6267\u884c\u4e00\u4e9b\u4e0e\u7c7b\u76f8\u5173\u4f46\u4e0d\u4f9d\u8d56\u4e8e\u7c7b\u5b9e\u4f8b\u5c5e\u6027\u7684\u64cd\u4f5c\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u4e2a\u7b80\u5355\u7684\u7c7b\u65b9\u6cd5\u793a\u4f8b\u53ef\u80fd\u662f\u4e00\u4e2a\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u6839\u636e\u4f20\u5165\u7684\u53c2\u6570\u6784\u9020\u7c7b\u7684\u5b9e\u4f8b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u8868\u793a\u65e5\u671f\u7684\u7c7b\uff0c\u6211\u4eec\u53ef\u80fd\u6709\u4e00\u4e2a\u7c7b\u65b9\u6cd5\u6765\u6839\u636eUnix\u65f6\u95f4\u6233\u521b\u5efa\u8fd9\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u7c7b\u65b9\u6cd5\u4e0e\u9759\u6001\u65b9\u6cd5\u548c\u5b9e\u4f8b\u65b9\u6cd5\u7684\u6bd4\u8f83<\/h2>\n<\/p>\n<p><p>\u7c7b\u65b9\u6cd5\u3001\u9759\u6001\u65b9\u6cd5\u548c\u5b9e\u4f8b\u65b9\u6cd5\u662fPython\u4e2d\u4e09\u79cd\u4e3b\u8981\u7684\u65b9\u6cd5\u7c7b\u578b\u3002\u5b83\u4eec\u4e4b\u95f4\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u5b83\u4eec\u662f\u5982\u4f55\u8bbf\u95ee\u7c7b\u548c\u5b9e\u4f8b\u7684\u5c5e\u6027\u7684\u3002<strong>\u5b9e\u4f8b\u65b9\u6cd5\u9700\u8981\u4e00\u4e2a\u5b9e\u4f8b\u548c\u5b9e\u4f8b\u7684\u5c5e\u6027\u6765\u6267\u884c\u3001\u9759\u6001\u65b9\u6cd5\u4e0d\u9700\u8981\u7c7b\u6216\u5b9e\u4f8b\u7684\u5f15\u7528\u3001\u7c7b\u65b9\u6cd5\u5219\u9700\u8981\u7c7b\u7684\u5f15\u7528<\/strong>\u3002 <\/p>\n<\/p>\n<p><p>\u7c7b\u65b9\u6cd5\u56e0\u5176\u80fd\u591f\u8bbf\u95ee\u7c7b\u7684\u5c5e\u6027\u800c\u7279\u522b\u6709\u7528\u3002\u8fd9\u4f7f\u5f97\u5b83\u4eec\u975e\u5e38\u9002\u5408\u4e8e\u9700\u8981\u6784\u9020\u7c7b\u5b9e\u4f8b\u6216\u9700\u8981\u8bbf\u95ee\u7c7b\u5c5e\u6027\u7684\u573a\u666f\uff0c\u4f8b\u5982\u7ef4\u62a4\u4e00\u4e2a\u7531\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\u7684\u6570\u636e\u8ba1\u6570\u5668\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u7c7b\u65b9\u6cd5\u7684\u5178\u578b\u5e94\u7528\u573a\u666f<\/h2>\n<\/p>\n<p><p>\u7c7b\u65b9\u6cd5\u6700\u5e7f\u6cdb\u7684\u5e94\u7528\u4e4b\u4e00\u662f\u5b9e\u73b0\u4f5c\u4e3a\u66ff\u4ee3\u6784\u9020\u51fd\u6570\u7684\u5de5\u5382\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u8f93\u5165\u53c2\u6570\u521b\u5efa\u7c7b\u7684\u4e0d\u540c\u5b9e\u4f8b\u3002\u4f8b\u5982\uff0c\u8003\u8651\u4e00\u4e2a\u56fe\u5f62\u754c\u9762\u5e93\u4e2d\u7684\u7a97\u53e3\u7c7b\uff0c\u53ef\u80fd\u6709\u591a\u79cd\u65b9\u6cd5\u6765\u521b\u5efa\u4e00\u4e2a\u7a97\u53e3\uff0c\u4f8b\u5982\u901a\u8fc7\u6307\u5b9a\u5927\u5c0f\u3001\u4f4d\u7f6e\u3001\u7a97\u53e3\u7c7b\u578b\u7b49\u3002\u901a\u8fc7\u4f7f\u7528\u7c7b\u65b9\u6cd5\u4f5c\u4e3a\u5de5\u5382\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a\u6e05\u6670\u4e14\u7075\u6d3b\u7684\u63a5\u53e3\u6765\u6784\u9020\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p>\u53e6\u4e00\u4e2a\u4f8b\u5b50\u662f\u5728\u6784\u5efa\u5355\u4f8b\u6a21\u5f0f\u65f6\u4f7f\u7528\u7c7b\u65b9\u6cd5\u3002\u5355\u4f8b\u6a21\u5f0f\u662f\u4e00\u79cd\u53ea\u5141\u8bb8\u521b\u5efa\u4e00\u4e2a\u7c7b\u5b9e\u4f8b\u7684\u8bbe\u8ba1\u6a21\u5f0f\u3002\u901a\u8fc7\u4f7f\u7528\u7c7b\u65b9\u6cd5\u68c0\u67e5\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u636e\u6b64\u8fd4\u56de\u8fd9\u4e2a\u5b9e\u4f8b\u6216\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5b9e\u73b0\u8fd9\u4e00\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u5728\u73b0\u4ee3Python\u4ee3\u7801\u4e2d\u4f7f\u7528\u7c7b\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u968f\u7740Python\u8bed\u8a00\u7684\u53d1\u5c55\uff0c\u7c7b\u65b9\u6cd5\u5728\u73b0\u4ee3\u4ee3\u7801\u4e2d\u4ecd\u7136\u626e\u6f14\u7740\u91cd\u8981\u7684\u89d2\u8272\u3002\u5b83\u4eec\u4e0d\u4ec5\u88ab\u7528\u4f5c\u5de5\u5382\u65b9\u6cd5\u6216\u5b9e\u73b0\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u8fd8\u88ab\u5e7f\u6cdb\u7528\u4e8e\u6846\u67b6\u548c\u5e93\u4e2d\u4ee5\u63d0\u4f9b\u7075\u6d3b\u7684API\u8bbe\u8ba1\u3002\u4f8b\u5982\uff0c\u5728Django\u6216Flask\u8fd9\u7c7bWeb\u6846\u67b6\u4e2d\uff0c\u7c7b\u65b9\u6cd5\u88ab\u7528\u6765\u6ce8\u518c\u89c6\u56fe\u6216\u6a21\u578b\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65e2\u7b80\u6d01\u53c8\u5f3a\u5927\u7684\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u7c7b\u65b9\u6cd5\u4e5f\u662f\u5b9e\u73b0\u5143\u7f16\u7a0b\u6280\u672f\u7684\u4e00\u90e8\u5206\uff0c\u4f8b\u5982\u5728\u521b\u5efa\u7c7b\u88c5\u9970\u5668\u65f6\u3002\u901a\u8fc7\u5728\u7c7b\u7ea7\u522b\u64cd\u4f5c\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u6dfb\u52a0\u3001\u4fee\u6539\u6216\u88c5\u9970\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u8fd9\u4e3aPython\u7f16\u7a0b\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\u548c\u52a8\u6001\u6027\u3002<\/p>\n<\/p>\n<p><p>\u7c7b\u65b9\u6cd5\u7684\u4f7f\u7528\uff0c\u7279\u522b\u662f\u4e0e\u5176\u4ed6\u7c7b\u578b\u7684\u65b9\u6cd5\uff08\u5982\u5b9e\u4f8b\u65b9\u6cd5\u548c\u9759\u6001\u65b9\u6cd5\uff09\u7684\u5408\u7406\u7ec4\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u6781\u5927\u5730\u589e\u5f3aPython\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u7075\u6d3b\u6027\u548c\u6548\u7387\u3002\u901a\u8fc7\u7406\u89e3\u8fd9\u4e9b\u4e0d\u540c\u7c7b\u578b\u7684\u65b9\u6cd5\u53ca\u5176\u5404\u81ea\u7684\u4f7f\u7528\u573a\u666f\uff0cPython\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u52a0\u6709\u6548\u5730\u8bbe\u8ba1\u548c\u5b9e\u73b0\u9762\u5411\u5bf9\u8c61\u7684\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u4e2d\u7684\u7c7b\u65b9\u6cd5\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528Python\u4e2d\u7684\u7c7b\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u5728\u65b9\u6cd5\u4e0a\u65b9\u6dfb\u52a0<code>@classmethod<\/code>\u88c5\u9970\u5668\u6765\u5b9e\u73b0\u3002\u7c7b\u65b9\u6cd5\u662f\u5728\u7c7b\u7ea7\u522b\u4e0a\u5b9a\u4e49\u7684\u65b9\u6cd5\uff0c\u800c\u4e0d\u662f\u5728\u5b9e\u4f8b\u7ea7\u522b\u4e0a\u5b9a\u4e49\u3002\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u8c03\u7528\u7c7b\u65b9\u6cd5\uff0c\u800c\u4e0d\u9700\u8981\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b\u3002<\/p>\n<p><strong>\u793a\u4f8b1\uff1a<\/strong><\/p>\n<pre><code class=\"language-python\">class MyClass:\n    @classmethod\n    def my_class_method(cls, arg1, arg2):\n        # \u5728\u7c7b\u65b9\u6cd5\u4e2d\u53ef\u4ee5\u4f7f\u7528\u7c7b\u53d8\u91cf\u548c\u5176\u4ed6\u7c7b\u65b9\u6cd5\n        # cls\u53c2\u6570\u6307\u5411\u7c7b\u672c\u8eab\n        return cls.__name__, arg1, arg2\n\n# \u8c03\u7528\u7c7b\u65b9\u6cd5\uff0c\u65e0\u9700\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b\nresult = MyClass.my_class_method(&quot;Hello&quot;, &quot;World&quot;)\nprint(result)  # \u8f93\u51fa\uff1a(&quot;MyClass&quot;, &quot;Hello&quot;, &quot;World&quot;)\n<\/code><\/pre>\n<p><strong>\u793a\u4f8b2\uff1a<\/strong><\/p>\n<pre><code class=\"language-python\">class Rectangle:\n    def __init__(self, width, height):\n        self.width = width\n        self.height = height\n    \n    @classmethod\n    def create_square(cls, side_length):\n        return cls(side_length, side_length)\n    \n    def area(self):\n        return self.width * self.height\n\n# \u4f7f\u7528\u7c7b\u65b9\u6cd5\u521b\u5efa\u6b63\u65b9\u5f62\u5bf9\u8c61\nsquare = Rectangle.create_square(5)\nprint(square.width, square.height)  # \u8f93\u51fa\uff1a5 5\nprint(square.area())  # \u8f93\u51fa\uff1a25\n<\/code><\/pre>\n<p><strong>\u6ce8\u610f\uff1a<\/strong> \u4f7f\u7528\u7c7b\u65b9\u6cd5\u65f6\u8981\u6ce8\u610f\u5728\u65b9\u6cd5\u5b9a\u4e49\u4e2d\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u547d\u540d\u4e3a<code>cls<\/code>\uff0c\u5b83\u6307\u5411\u7c7b\u672c\u8eab\u3002\u901a\u8fc7<code>cls<\/code>\u53c2\u6570\u53ef\u4ee5\u8bbf\u95ee\u7c7b\u7684\u5c5e\u6027\u548c\u5176\u4ed6\u7c7b\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7c7b\u65b9\u6cd5\u5728Python\u4e2d\u662f\u4e00\u79cd\u4f9d\u9644\u4e8e\u7c7b\u800c\u975e\u7c7b\u7684\u5b9e\u4f8b\u7684\u65b9\u6cd5\u3002\u5b83\u4eec\u7684\u6700\u5927\u7279\u70b9\u662f\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u7c7b\u672c\u8eab\uff0c\u901a\u5e38\u7528cls\u4f5c\u4e3a [&hellip;]","protected":false},"author":3,"featured_media":176155,"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\/176145"}],"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=176145"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/176145\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/176155"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=176145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=176145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=176145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}