{"id":1127647,"date":"2025-01-08T20:12:17","date_gmt":"2025-01-08T12:12:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1127647.html"},"modified":"2025-01-08T20:12:20","modified_gmt":"2025-01-08T12:12:20","slug":"python%e7%b1%bb%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8%e7%b1%bb%e5%87%bd%e6%95%b0%e8%bf%94%e5%9b%9e%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1127647.html","title":{"rendered":"python\u7c7b\u51fd\u6570\u5982\u4f55\u8c03\u7528\u7c7b\u51fd\u6570\u8fd4\u56de\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094616\/a9fbdf2c-8a7a-4111-9f82-90270c6120c2.webp\" alt=\"python\u7c7b\u51fd\u6570\u5982\u4f55\u8c03\u7528\u7c7b\u51fd\u6570\u8fd4\u56de\u503c\" \/><\/p>\n<p><p> <strong>Python\u7c7b\u51fd\u6570\u7684\u8c03\u7528\u53ca\u8fd4\u56de\u503c\u83b7\u53d6\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5b9e\u4f8b\u65b9\u6cd5\u3001\u7c7b\u65b9\u6cd5\u3001\u9759\u6001\u65b9\u6cd5\u7b49\u3002\u5b9e\u4f8b\u65b9\u6cd5\u9700\u8981\u5b9e\u4f8b\u5316\u5bf9\u8c61\u540e\u8c03\u7528\u3001\u7c7b\u65b9\u6cd5\u4f7f\u7528@classmethod\u88c5\u9970\u5668\u3001\u9759\u6001\u65b9\u6cd5\u4f7f\u7528@staticmethod\u88c5\u9970\u5668\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u79cd\u65b9\u6cd5\u53ca\u5176\u533a\u522b\uff0c\u5e76\u7ed9\u51fa\u793a\u4f8b\u4ee3\u7801\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b9e\u4f8b\u65b9\u6cd5\u7684\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u5b9e\u4f8b\u65b9\u6cd5\u662f\u6307\u5c5e\u4e8e\u67d0\u4e2a\u7c7b\u7684\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002\u8981\u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u8be5\u7c7b\u7684\u5bf9\u8c61\uff0c\u7136\u540e\u901a\u8fc7\u5bf9\u8c61\u6765\u8c03\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def instance_method(self):<\/p>\n<p>        return &quot;Instance Method Called&quot;<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>obj = MyClass()<\/p>\n<h2><strong>\u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5<\/strong><\/h2>\n<p>result = obj.instance_method()<\/p>\n<p>print(result)  # \u8f93\u51fa: Instance Method Called<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>instance_method<\/code>\u662f\u4e00\u4e2a\u5b9e\u4f8b\u65b9\u6cd5\u3002\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u7c7b<code>MyClass<\/code>\u7684\u5b9e\u4f8b<code>obj<\/code>\uff0c\u7136\u540e\u901a\u8fc7<code>obj<\/code>\u6765\u8c03\u7528<code>instance_method<\/code>\uff0c\u5e76\u8f93\u51fa\u8fd4\u56de\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7c7b\u65b9\u6cd5\u7684\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u7c7b\u65b9\u6cd5\u662f\u5c5e\u4e8e\u7c7b\u672c\u8eab\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u7c7b\u6216\u7c7b\u7684\u5b9e\u4f8b\u6765\u8c03\u7528\u3002\u7c7b\u65b9\u6cd5\u4f7f\u7528<code>@classmethod<\/code>\u88c5\u9970\u5668\u6765\u5b9a\u4e49\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u901a\u5e38\u547d\u540d\u4e3a<code>cls<\/code>\uff0c\u4ee3\u8868\u7c7b\u672c\u8eab\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    @classmethod<\/p>\n<p>    def class_method(cls):<\/p>\n<p>        return &quot;Class Method Called&quot;<\/p>\n<h2><strong>\u901a\u8fc7\u7c7b\u540d\u8c03\u7528\u7c7b\u65b9\u6cd5<\/strong><\/h2>\n<p>result = MyClass.class_method()<\/p>\n<p>print(result)  # \u8f93\u51fa: Class Method Called<\/p>\n<h2><strong>\u901a\u8fc7\u5b9e\u4f8b\u8c03\u7528\u7c7b\u65b9\u6cd5<\/strong><\/h2>\n<p>obj = MyClass()<\/p>\n<p>result = obj.class_method()<\/p>\n<p>print(result)  # \u8f93\u51fa: Class Method Called<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>class_method<\/code>\u662f\u4e00\u4e2a\u7c7b\u65b9\u6cd5\u3002\u6211\u4eec\u65e2\u53ef\u4ee5\u901a\u8fc7\u7c7b<code>MyClass<\/code>\u6765\u8c03\u7528\u5b83\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u7c7b\u7684\u5b9e\u4f8b<code>obj<\/code>\u6765\u8c03\u7528\u5b83\uff0c\u7ed3\u679c\u90fd\u662f\u4e00\u6837\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u9759\u6001\u65b9\u6cd5\u7684\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u9759\u6001\u65b9\u6cd5\u662f\u5c5e\u4e8e\u7c7b\u7684\uff0c\u4f46\u4e0d\u4f9d\u8d56\u4e8e\u7c7b\u7684\u5b9e\u4f8b\u3002\u9759\u6001\u65b9\u6cd5\u4f7f\u7528<code>@staticmethod<\/code>\u88c5\u9970\u5668\u6765\u5b9a\u4e49\uff0c\u4e0d\u9700\u8981\u4f20\u9012<code>self<\/code>\u6216<code>cls<\/code>\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    @staticmethod<\/p>\n<p>    def static_method():<\/p>\n<p>        return &quot;Static Method Called&quot;<\/p>\n<h2><strong>\u901a\u8fc7\u7c7b\u540d\u8c03\u7528\u9759\u6001\u65b9\u6cd5<\/strong><\/h2>\n<p>result = MyClass.static_method()<\/p>\n<p>print(result)  # \u8f93\u51fa: Static Method Called<\/p>\n<h2><strong>\u901a\u8fc7\u5b9e\u4f8b\u8c03\u7528\u9759\u6001\u65b9\u6cd5<\/strong><\/h2>\n<p>obj = MyClass()<\/p>\n<p>result = obj.static_method()<\/p>\n<p>print(result)  # \u8f93\u51fa: Static Method Called<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>static_method<\/code>\u662f\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\u3002\u6211\u4eec\u65e2\u53ef\u4ee5\u901a\u8fc7\u7c7b<code>MyClass<\/code>\u6765\u8c03\u7528\u5b83\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u7c7b\u7684\u5b9e\u4f8b<code>obj<\/code>\u6765\u8c03\u7528\u5b83\uff0c\u7ed3\u679c\u90fd\u662f\u4e00\u6837\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u540c\u4e00\u4e2a\u7c7b\u4e2d\u5b9a\u4e49\u548c\u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5\u3001\u7c7b\u65b9\u6cd5\u548c\u9759\u6001\u65b9\u6cd5\uff0c\u5e76\u83b7\u53d6\u5b83\u4eec\u7684\u8fd4\u56de\u503c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def instance_method(self):<\/p>\n<p>        return &quot;Instance Method Called&quot;<\/p>\n<p>    @classmethod<\/p>\n<p>    def class_method(cls):<\/p>\n<p>        return &quot;Class Method Called&quot;<\/p>\n<p>    @staticmethod<\/p>\n<p>    def static_method():<\/p>\n<p>        return &quot;Static Method Called&quot;<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>obj = MyClass()<\/p>\n<h2><strong>\u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5<\/strong><\/h2>\n<p>result_instance = obj.instance_method()<\/p>\n<p>print(result_instance)  # \u8f93\u51fa: Instance Method Called<\/p>\n<h2><strong>\u8c03\u7528\u7c7b\u65b9\u6cd5<\/strong><\/h2>\n<p>result_class = MyClass.class_method()<\/p>\n<p>print(result_class)  # \u8f93\u51fa: Class Method Called<\/p>\n<h2><strong>\u8c03\u7528\u9759\u6001\u65b9\u6cd5<\/strong><\/h2>\n<p>result_static = MyClass.static_method()<\/p>\n<p>print(result_static)  # \u8f93\u51fa: Static Method Called<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b<code>MyClass<\/code>\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u5b9e\u4f8b\u65b9\u6cd5\u3001\u4e00\u4e2a\u7c7b\u65b9\u6cd5\u548c\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\u3002\u6211\u4eec\u5206\u522b\u8c03\u7528\u4e86\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u83b7\u53d6\u5b83\u4eec\u7684\u8fd4\u56de\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u4f8b\u65b9\u6cd5\u3001\u7c7b\u65b9\u6cd5\u4e0e\u9759\u6001\u65b9\u6cd5\u7684\u533a\u522b<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b9e\u4f8b\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u901a\u8fc7\u7c7b\u7684\u5b9e\u4f8b\u6765\u8c03\u7528\u3002<\/li>\n<li>\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f<code>self<\/code>\uff0c\u6307\u5411\u7c7b\u7684\u5b9e\u4f8b\u3002<\/li>\n<li>\u53ef\u4ee5\u8bbf\u95ee\u548c\u4fee\u6539\u5b9e\u4f8b\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7c7b\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u53ef\u4ee5\u901a\u8fc7\u7c7b\u6216\u7c7b\u7684\u5b9e\u4f8b\u6765\u8c03\u7528\u3002<\/li>\n<li>\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f<code>cls<\/code>\uff0c\u6307\u5411\u7c7b\u672c\u8eab\u3002<\/li>\n<li>\u4e3b\u8981\u7528\u4e8e\u64cd\u4f5c\u7c7b\u5c5e\u6027\u6216\u7c7b\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u9759\u6001\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u53ef\u4ee5\u901a\u8fc7\u7c7b\u6216\u7c7b\u7684\u5b9e\u4f8b\u6765\u8c03\u7528\u3002<\/li>\n<li>\u4e0d\u9700\u8981\u4f20\u9012<code>self<\/code>\u6216<code>cls<\/code>\u53c2\u6570\u3002<\/li>\n<li>\u4e3b\u8981\u7528\u4e8e\u903b\u8f91\u4e0a\u5c5e\u4e8e\u7c7b\u7684\u51fd\u6570\uff0c\u4f46\u4e0d\u9700\u8981\u8bbf\u95ee\u7c7b\u6216\u5b9e\u4f8b\u5c5e\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e09\u79cd\u65b9\u6cd5\u7684\u4f7f\u7528\u65b9\u5f0f\u548c\u533a\u522b\uff0c\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u7f16\u5199\u548c\u7ba1\u7406\u7c7b\u4e2d\u7684\u65b9\u6cd5\uff0c\u4f7f\u4ee3\u7801\u7ed3\u6784\u66f4\u52a0\u6e05\u6670\u548c\u5408\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u548c\u8c03\u7528\u7c7b\u4e2d\u7684\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u7c7b\u51fd\u6570\u65f6\uff0c\u9700\u8981\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\u3002\u5728\u7c7b\u5185\u90e8\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9e\u4f8b\u5316\u5bf9\u8c61\u6765\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\u3002\u8c03\u7528\u65f6\uff0c\u4f7f\u7528\u5bf9\u8c61\u540d\u52a0\u4e0a\u51fd\u6570\u540d\uff0c\u5e76\u5728\u540e\u9762\u52a0\u4e0a\u62ec\u53f7\u3002\u4f8b\u5982\uff1a<code>my_object.my_function()<\/code>\u3002\u5982\u679c\u51fd\u6570\u6709\u8fd4\u56de\u503c\uff0c\u53ef\u4ee5\u5c06\u5176\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\u4ee5\u4fbf\u540e\u7eed\u4f7f\u7528\u3002<\/p>\n<p><strong>\u7c7b\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u53ef\u4ee5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u7c7b\u51fd\u6570\u7684\u8fd4\u56de\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u57fa\u672c\u7c7b\u578b\uff08\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u548c\u5b57\u7b26\u4e32\uff09\u548c\u590d\u6742\u7c7b\u578b\uff08\u5982\u5217\u8868\u3001\u5b57\u5178\u548c\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff09\u3002\u5177\u4f53\u8fd4\u56de\u4ec0\u4e48\u7c7b\u578b\u53d6\u51b3\u4e8e\u51fd\u6570\u5185\u90e8\u7684\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u8ba1\u7b97\u7ed3\u679c\uff0c\u6216\u8005\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u7684\u72b6\u6001\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7c7b\u51fd\u6570\u8fd4\u56de\u503c\u4e2d\u7684\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u8c03\u7528\u7c7b\u51fd\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5f02\u5e38\u60c5\u51b5\u3002\u53ef\u4ee5\u901a\u8fc7<code>try...except<\/code>\u8bed\u53e5\u6765\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002\u5728\u51fd\u6570\u5185\u90e8\uff0c\u53ef\u4ee5\u4f7f\u7528<code>r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se<\/code>\u8bed\u53e5\u4e3b\u52a8\u629b\u51fa\u5f02\u5e38\uff0c\u4ee5\u4fbf\u5728\u8c03\u7528\u65f6\u6355\u83b7\u5e76\u5904\u7406\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\uff0c\u5e76\u63d0\u4f9b\u7528\u6237\u53cb\u597d\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7c7b\u51fd\u6570\u7684\u8c03\u7528\u53ca\u8fd4\u56de\u503c\u83b7\u53d6\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5b9e\u4f8b\u65b9\u6cd5\u3001\u7c7b\u65b9\u6cd5\u3001\u9759\u6001\u65b9\u6cd5\u7b49\u3002\u5b9e\u4f8b\u65b9\u6cd5\u9700\u8981\u5b9e\u4f8b\u5316\u5bf9 [&hellip;]","protected":false},"author":3,"featured_media":1127650,"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\/1127647"}],"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=1127647"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1127647\/revisions"}],"predecessor-version":[{"id":1127652,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1127647\/revisions\/1127652"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1127650"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1127647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1127647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1127647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}