{"id":1160978,"date":"2025-01-13T19:11:18","date_gmt":"2025-01-13T11:11:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1160978.html"},"modified":"2025-01-13T19:11:21","modified_gmt":"2025-01-13T11:11:21","slug":"python-%e7%b1%bb%e4%b8%ad%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1160978.html","title":{"rendered":"python \u7c7b\u4e2d\u5982\u4f55\u8c03\u7528\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25202351\/7593c345-3f3f-4297-b54c-d1825b4f8c4d.webp\" alt=\"python \u7c7b\u4e2d\u5982\u4f55\u8c03\u7528\u51fd\u6570\" \/><\/p>\n<p><p> \u5728Python\u7c7b\u4e2d\uff0c\u8c03\u7528\u51fd\u6570\u901a\u5e38\u662f\u901a\u8fc7\u7c7b\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u7684\u3002<strong>\u7c7b\u7684\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u7c7b\u7684\u5b9e\u4f8b\u6765\u8c03\u7528\u3001\u7c7b\u7684\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u7c7b\u672c\u8eab\u6765\u8c03\u7528\u3001\u7c7b\u7684\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u65b9\u6cd5\u6765\u8c03\u7528<\/strong>\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u5373\u901a\u8fc7\u7c7b\u7684\u5b9e\u4f8b\u6765\u8c03\u7528\u7c7b\u4e2d\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7c7b\u7684\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u7c7b\u7684\u5b9e\u4f8b\u6765\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u7c7b\u65b9\u6cd5\u662f\u901a\u8fc7\u7c7b\u7684\u5b9e\u4f8b\u6765\u8c03\u7528\u7684\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u5e76\u5728\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e9b\u65b9\u6cd5\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u8fd9\u4e2a\u7c7b\u7684\u5b9e\u4f8b\uff0c\u5e76\u901a\u8fc7\u5b9e\u4f8b\u6765\u8c03\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(f&quot;Hello, {self.name}!&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>my_instance = MyClass(&quot;Alice&quot;)<\/p>\n<h2><strong>\u901a\u8fc7\u5b9e\u4f8b\u8c03\u7528\u7c7b\u7684\u65b9\u6cd5<\/strong><\/h2>\n<p>my_instance.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>MyClass<\/code>\u7684\u7c7b\uff0c\u5e76\u5728\u7c7b\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>greet<\/code>\u7684\u65b9\u6cd5\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u8fd9\u4e2a\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u901a\u8fc7\u5b9e\u4f8b\u8c03\u7528\u4e86<code>greet<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7c7b\u7684\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u7c7b\u672c\u8eab\u6765\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5728\u7c7b\u7684\u5185\u90e8\u8c03\u7528\u5176\u4ed6\u65b9\u6cd5\u3002\u8fd9\u901a\u5e38\u662f\u5728\u9700\u8981\u5c06\u903b\u8f91\u5206\u89e3\u4e3a\u591a\u4e2a\u65b9\u6cd5\u65f6\u53d1\u751f\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(f&quot;Hello, {self.name}!&quot;)<\/p>\n<p>    def say_goodbye(self):<\/p>\n<p>        print(f&quot;Goodbye, {self.name}!&quot;)<\/p>\n<p>    def full_greeting(self):<\/p>\n<p>        self.greet()<\/p>\n<p>        self.say_goodbye()<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>my_instance = MyClass(&quot;Alice&quot;)<\/p>\n<h2><strong>\u901a\u8fc7\u5b9e\u4f8b\u8c03\u7528\u7c7b\u7684\u65b9\u6cd5<\/strong><\/h2>\n<p>my_instance.full_greeting()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728<code>MyClass<\/code>\u7c7b\u4e2d\u5b9a\u4e49\u4e86\u4e09\u4e2a\u65b9\u6cd5\uff1a<code>greet<\/code>\u3001<code>say_goodbye<\/code>\u548c<code>full_greeting<\/code>\u3002\u5728<code>full_greeting<\/code>\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u8c03\u7528\u4e86<code>greet<\/code>\u548c<code>say_goodbye<\/code>\u65b9\u6cd5\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u8fd9\u4e2a\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u901a\u8fc7\u5b9e\u4f8b\u8c03\u7528\u4e86<code>full_greeting<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7c7b\u7684\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u65b9\u6cd5\u6765\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u4ece\u7c7b\u7684\u4e00\u4e2a\u65b9\u6cd5\u8c03\u7528\u53e6\u4e00\u4e2a\u65b9\u6cd5\u3002\u8fd9\u5728\u65b9\u6cd5\u9700\u8981\u5171\u4eab\u67d0\u4e9b\u903b\u8f91\u65f6\u7279\u522b\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(f&quot;Hello, {self.name}!&quot;)<\/p>\n<p>    def greet_formally(self):<\/p>\n<p>        self.greet()<\/p>\n<p>        print(&quot;It&#39;s a pleasure to meet you.&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>my_instance = MyClass(&quot;Alice&quot;)<\/p>\n<h2><strong>\u901a\u8fc7\u5b9e\u4f8b\u8c03\u7528\u7c7b\u7684\u65b9\u6cd5<\/strong><\/h2>\n<p>my_instance.greet_formally()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>greet_formally<\/code>\u65b9\u6cd5\u8c03\u7528\u4e86<code>greet<\/code>\u65b9\u6cd5\uff0c\u5e76\u5728\u8c03\u7528\u540e\u6dfb\u52a0\u4e86\u989d\u5916\u7684\u6d88\u606f\u3002\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u4e86\u6211\u4eec\u4e0d\u4f1a\u91cd\u590d\u76f8\u540c\u7684\u4ee3\u7801\uff0c\u5e76\u4f7f\u5f97\u903b\u8f91\u66f4\u52a0\u6a21\u5757\u5316\u548c\u53ef\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5176\u4ed6\u8c03\u7528\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u4e09\u79cd\u4e3b\u8981\u65b9\u6cd5\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u65b9\u5f0f\u53ef\u4ee5\u5728\u7c7b\u4e2d\u8c03\u7528\u51fd\u6570\uff0c\u6bd4\u5982\u901a\u8fc7\u7c7b\u65b9\u6cd5\u3001\u9759\u6001\u65b9\u6cd5\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1. \u7c7b\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u7c7b\u65b9\u6cd5\u662f\u7ed1\u5b9a\u5230\u7c7b\u800c\u4e0d\u662f\u7c7b\u7684\u5b9e\u4f8b\u7684\u65b9\u6cd5\u3002\u5b83\u4eec\u53ef\u4ee5\u901a\u8fc7\u7c7b\u672c\u8eab\u6765\u8c03\u7528\u3002\u8981\u5b9a\u4e49\u7c7b\u65b9\u6cd5\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528<code>@classmethod<\/code>\u88c5\u9970\u5668\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    name = &quot;Class Name&quot;<\/p>\n<p>    @classmethod<\/p>\n<p>    def class_greet(cls):<\/p>\n<p>        print(f&quot;Hello from {cls.name}!&quot;)<\/p>\n<h2><strong>\u901a\u8fc7\u7c7b\u8c03\u7528\u7c7b\u65b9\u6cd5<\/strong><\/h2>\n<p>MyClass.class_greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u9759\u6001\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u9759\u6001\u65b9\u6cd5\u662f\u4e0d\u4f9d\u8d56\u4e8e\u7c7b\u5b9e\u4f8b\u7684\u65b9\u6cd5\u3002\u5b83\u4eec\u53ef\u4ee5\u901a\u8fc7\u7c7b\u672c\u8eab\u6216\u7c7b\u7684\u5b9e\u4f8b\u6765\u8c03\u7528\u3002\u8981\u5b9a\u4e49\u9759\u6001\u65b9\u6cd5\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528<code>@staticmethod<\/code>\u88c5\u9970\u5668\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    @staticmethod<\/p>\n<p>    def static_greet():<\/p>\n<p>        print(&quot;Hello from static method!&quot;)<\/p>\n<h2><strong>\u901a\u8fc7\u7c7b\u8c03\u7528\u9759\u6001\u65b9\u6cd5<\/strong><\/h2>\n<p>MyClass.static_greet()<\/p>\n<h2><strong>\u901a\u8fc7\u5b9e\u4f8b\u8c03\u7528\u9759\u6001\u65b9\u6cd5<\/strong><\/h2>\n<p>my_instance = MyClass()<\/p>\n<p>my_instance.static_greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u7c7b\u4e2d\u7684\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u8c03\u7528\uff0c<strong>\u5305\u62ec\u901a\u8fc7\u7c7b\u7684\u5b9e\u4f8b\u8c03\u7528\u3001\u901a\u8fc7\u7c7b\u672c\u8eab\u8c03\u7528\u3001\u901a\u8fc7\u5176\u4ed6\u65b9\u6cd5\u8c03\u7528\u3001\u7c7b\u65b9\u6cd5\u548c\u9759\u6001\u65b9\u6cd5<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7075\u6d3b\u548c\u6a21\u5757\u5316\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u7406\u89e3\u5e76\u638c\u63e1\u8fd9\u4e9b\u4e0d\u540c\u7684\u8c03\u7528\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u7f16\u5199Python\u7c7b\u65f6\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7c7b\u4e2d\u5b9a\u4e49\u548c\u8c03\u7528\u65b9\u6cd5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u7c7b\u7684\u65b9\u6cd5\u662f\u5b9a\u4e49\u5728\u7c7b\u5185\u90e8\u7684\u51fd\u6570\u3002\u8981\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u7c7b\u4e2d\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\uff0c\u5e76\u786e\u4fdd\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f<code>self<\/code>\uff0c\u5b83\u8868\u793a\u5bf9\u8c61\u81ea\u8eab\u3002\u8981\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b\u5e76\u4f7f\u7528\u5b9e\u4f8b\u8c03\u7528\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">class MyClass:\n    def my_method(self):\n        print(&quot;Hello, World!&quot;)\n\nobj = MyClass()\nobj.my_method()  # \u8c03\u7528\u65b9\u6cd5\n<\/code><\/pre>\n<p><strong>\u5728\u7c7b\u7684\u5b9e\u4f8b\u4e2d\u5982\u4f55\u8bbf\u95ee\u5176\u4ed6\u65b9\u6cd5\uff1f<\/strong><br \/>\u5728\u7c7b\u7684\u4e00\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>self<\/code>\u5173\u952e\u5b57\u8bbf\u95ee\u540c\u4e00\u7c7b\u4e2d\u7684\u5176\u4ed6\u65b9\u6cd5\u3002\u8fd9\u6837\u53ef\u4ee5\u5b9e\u73b0\u65b9\u6cd5\u4e4b\u95f4\u7684\u4ea4\u4e92\u548c\u6570\u636e\u4f20\u9012\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">class MyClass:\n    def method_one(self):\n        return &quot;Method One&quot;\n\n    def method_two(self):\n        result = self.method_one()  # \u8c03\u7528\u53e6\u4e00\u4e2a\u65b9\u6cd5\n        print(result)\n\nobj = MyClass()\nobj.method_two()  # \u8f93\u51fa\uff1aMethod One\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728\u7c7b\u4e2d\u4f7f\u7528\u9759\u6001\u65b9\u6cd5\u548c\u7c7b\u65b9\u6cd5\uff1f<\/strong><br \/>Python\u4e2d\u63d0\u4f9b\u4e86<code>@staticmethod<\/code>\u548c<code>@classmethod<\/code>\u88c5\u9970\u5668\u6765\u5b9a\u4e49\u9759\u6001\u65b9\u6cd5\u548c\u7c7b\u65b9\u6cd5\u3002\u9759\u6001\u65b9\u6cd5\u4e0d\u9700\u8981\u8bbf\u95ee\u7c7b\u6216\u5b9e\u4f8b\u7684\u5c5e\u6027\uff0c\u800c\u7c7b\u65b9\u6cd5\u5219\u53ef\u4ee5\u8bbf\u95ee\u7c7b\u7684\u5c5e\u6027\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">class MyClass:\n    @staticmethod\n    def static_method():\n        print(&quot;This is a static method.&quot;)\n\n    @classmethod\n    def class_method(cls):\n        print(&quot;This is a class method.&quot;)\n\nMyClass.static_method()  # \u8c03\u7528\u9759\u6001\u65b9\u6cd5\nMyClass.class_method()   # \u8c03\u7528\u7c7b\u65b9\u6cd5\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u7075\u6d3b\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406\u7c7b\u4e2d\u7684\u51fd\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u7c7b\u4e2d\uff0c\u8c03\u7528\u51fd\u6570\u901a\u5e38\u662f\u901a\u8fc7\u7c7b\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u7684\u3002\u7c7b\u7684\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u7c7b\u7684\u5b9e\u4f8b\u6765\u8c03\u7528\u3001\u7c7b\u7684\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u7c7b\u672c\u8eab [&hellip;]","protected":false},"author":3,"featured_media":1160989,"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\/1160978"}],"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=1160978"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160978\/revisions"}],"predecessor-version":[{"id":1160991,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160978\/revisions\/1160991"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1160989"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1160978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1160978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1160978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}