{"id":1155526,"date":"2025-01-13T18:04:33","date_gmt":"2025-01-13T10:04:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1155526.html"},"modified":"2025-01-13T18:04:36","modified_gmt":"2025-01-13T10:04:36","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8class%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1155526.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528class\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25194119\/2d3effd8-cc29-4443-96ae-2cd7767dd12e.webp\" alt=\"python\u5982\u4f55\u8c03\u7528class\u6587\u4ef6\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8c03\u7528class\u6587\u4ef6\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec<strong>\u5bfc\u5165\u6a21\u5757\u3001\u5b9e\u4f8b\u5316\u7c7b\u3001\u8c03\u7528\u7c7b\u65b9\u6cd5\u548c\u5c5e\u6027<\/strong>\u3002<strong>\u5bfc\u5165\u6a21\u5757<\/strong>\u662f\u6307\u4f7f\u7528import\u8bed\u53e5\u5c06class\u6587\u4ef6\u5f15\u5165\u5230\u5f53\u524d\u811a\u672c\u4e2d\uff0c<strong>\u5b9e\u4f8b\u5316\u7c7b<\/strong>\u662f\u6307\u521b\u5efa\u7c7b\u7684\u5bf9\u8c61\uff0c<strong>\u8c03\u7528\u7c7b\u65b9\u6cd5\u548c\u5c5e\u6027<\/strong>\u662f\u6307\u4f7f\u7528\u70b9\u64cd\u4f5c\u7b26\u6765\u8bbf\u95ee\u7c7b\u4e2d\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5bfc\u5165\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>\u5bfc\u5165\u6a21\u5757\u662f\u8c03\u7528class\u6587\u4ef6\u7684\u7b2c\u4e00\u6b65\u3002\u5728Python\u4e2d\uff0c\u4f7f\u7528import\u8bed\u53e5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6a21\u5757\u6216\u5305\u5f15\u5165\u5230\u5f53\u524d\u811a\u672c\u4e2d\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2aclass\u6587\u4ef6\u540d\u4e3amy_class.py\uff0c\u5e76\u4e14\u8be5\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3aMyClass\u7684\u7c7b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5bfc\u5165\u5e76\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from my_class import MyClass<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>my_instance = MyClass()<\/p>\n<h2><strong>\u8c03\u7528\u7c7b\u7684\u65b9\u6cd5<\/strong><\/h2>\n<p>my_instance.my_method()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8Python\u5982\u4f55\u8c03\u7528class\u6587\u4ef6\u7684\u5404\u4e2a\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528import\u8bed\u53e5<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0cimport\u8bed\u53e5\u7528\u4e8e\u5f15\u5165\u4e00\u4e2a\u6a21\u5757\u6216\u5305\u3002\u901a\u8fc7import\uff0c\u53ef\u4ee5\u8bbf\u95ee\u6a21\u5757\u6216\u5305\u4e2d\u7684\u7c7b\u3001\u51fd\u6570\u3001\u53d8\u91cf\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import my_class<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u4e0a\u8ff0\u8bed\u53e5\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>my_class.MyClass<\/code> \u6765\u8bbf\u95eeMyClass\u7c7b\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528from&#8230;import\u8bed\u53e5<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u53ea\u9700\u8981\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u67d0\u4e2a\u7279\u5b9a\u7c7b\u6216\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528from&#8230;import\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from my_class import MyClass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 <code>MyClass<\/code> \u6765\u521b\u5efa\u5b9e\u4f8b\u548c\u8c03\u7528\u65b9\u6cd5\uff0c\u800c\u65e0\u9700\u6307\u5b9a\u6a21\u5757\u540d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b9e\u4f8b\u5316\u7c7b<\/h3>\n<\/p>\n<p><p>\u5b9e\u4f8b\u5316\u7c7b\u662f\u521b\u5efa\u7c7b\u7684\u5bf9\u8c61\u3002\u901a\u8fc7\u8c03\u7528\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u53ef\u4ee5\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_instance = MyClass()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u5b9e\u4f8b\u65f6\uff0c\u6784\u9020\u51fd\u6570\uff08\u5373<code>__init__<\/code>\u65b9\u6cd5\uff09\u4f1a\u88ab\u81ea\u52a8\u8c03\u7528\u3002\u53ef\u4ee5\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f20\u9012\u53c2\u6570\u6765\u521d\u59cb\u5316\u5bf9\u8c61\u3002<\/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>my_instance = MyClass(&quot;Alice&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8c03\u7528\u7c7b\u65b9\u6cd5\u548c\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u70b9\u64cd\u4f5c\u7b26\u6765\u8bbf\u95ee\u7c7b\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u7c7b<\/p>\n<p>class MyClass:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def greet(self):<\/p>\n<p>        return f&quot;Hello, {self.name}!&quot;<\/p>\n<h2><strong>\u521b\u5efa\u5b9e\u4f8b<\/strong><\/h2>\n<p>my_instance = MyClass(&quot;Alice&quot;)<\/p>\n<h2><strong>\u8c03\u7528\u65b9\u6cd5<\/strong><\/h2>\n<p>print(my_instance.greet())  # \u8f93\u51fa: Hello, Alice!<\/p>\n<h2><strong>\u8bbf\u95ee\u5c5e\u6027<\/strong><\/h2>\n<p>print(my_instance.name)  # \u8f93\u51fa: Alice<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b8c\u6574\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u7ed3\u5408\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u8c03\u7528class\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6709\u4e00\u4e2a\u6587\u4ef6\u540d\u4e3amy_class.py\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_class.py<\/p>\n<p>class MyClass:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def greet(self):<\/p>\n<p>        return f&quot;Hello, {self.name}!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u53e6\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u5bfc\u5165\u5e76\u4f7f\u7528MyClass\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/p>\n<p>from my_class import MyClass<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>my_instance = MyClass(&quot;Alice&quot;)<\/p>\n<h2><strong>\u8c03\u7528\u7c7b\u7684\u65b9\u6cd5<\/strong><\/h2>\n<p>print(my_instance.greet())  # \u8f93\u51fa: Hello, Alice!<\/p>\n<h2><strong>\u8bbf\u95ee\u7c7b\u7684\u5c5e\u6027<\/strong><\/h2>\n<p>print(my_instance.name)  # \u8f93\u51fa: Alice<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6a21\u5757\u8def\u5f84\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u5f53\u6a21\u5757\u4f4d\u4e8e\u4e0d\u540c\u76ee\u5f55\u65f6\uff0c\u9700\u8981\u8bbe\u7f6ePython\u8def\u5f84\u6216\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bbe\u7f6ePython\u8def\u5f84<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u6a21\u5757\u6240\u5728\u76ee\u5f55\u6dfb\u52a0\u5230Python\u8def\u5f84\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>sys.path.append(&#39;\/path\/to\/directory&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6a21\u5757\u5728\u540c\u4e00\u5305\u5185\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from .my_class import MyClass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528__main__\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u5728\u6a21\u5757\u88ab\u76f4\u63a5\u8fd0\u884c\u65f6\u6267\u884c\u67d0\u4e9b\u4ee3\u7801\uff0c\u800c\u5728\u88ab\u5bfc\u5165\u65f6\u4e0d\u6267\u884c\u8fd9\u4e9b\u4ee3\u7801\u3002\u53ef\u4ee5\u4f7f\u7528 <code>__name__<\/code> \u5c5e\u6027\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_class.py<\/p>\n<p>class MyClass:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def greet(self):<\/p>\n<p>        return f&quot;Hello, {self.name}!&quot;<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    # \u4ec5\u5f53\u6a21\u5757\u88ab\u76f4\u63a5\u8fd0\u884c\u65f6\u6267\u884c<\/p>\n<p>    my_instance = MyClass(&quot;Alice&quot;)<\/p>\n<p>    print(my_instance.greet())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u8de8\u6a21\u5757\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5728\u591a\u4e2a\u6a21\u5757\u4e4b\u95f4\u8c03\u7528\u7c7b\u548c\u51fd\u6570\u3002\u53ef\u4ee5\u901a\u8fc7\u5305\u548c\u6a21\u5757\u7ec4\u7ec7\u4ee3\u7801\uff0c\u5e76\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u548c\u7edd\u5bf9\u5bfc\u5165\u8fdb\u884c\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u9879\u76ee\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>    \u251c\u2500\u2500 package\/<\/p>\n<p>    \u2502   \u251c\u2500\u2500 __init__.py<\/p>\n<p>    \u2502   \u251c\u2500\u2500 module_a.py<\/p>\n<p>    \u2502   \u2514\u2500\u2500 module_b.py<\/p>\n<p>    \u2514\u2500\u2500 main.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728module_a.py\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># package\/module_a.py<\/p>\n<p>class MyClassA:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def greet(self):<\/p>\n<p>        return f&quot;Hello, {self.name} from Module A!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728module_b.py\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528MyClassA\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># package\/module_b.py<\/p>\n<p>from .module_a import MyClassA<\/p>\n<p>class MyClassB:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.instance_a = MyClassA(name)<\/p>\n<p>    def greet(self):<\/p>\n<p>        return self.instance_a.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728main.py\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528MyClassB\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from package.module_b import MyClassB<\/p>\n<p>my_instance_b = MyClassB(&quot;Alice&quot;)<\/p>\n<p>print(my_instance_b.greet())  # \u8f93\u51fa: Hello, Alice from Module A!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6765\u7ba1\u7406\u548c\u8c03\u7528\u7c7b\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 importlib \u6765\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import importlib<\/p>\n<p>module_name = &#39;my_class&#39;<\/p>\n<p>class_name = &#39;MyClass&#39;<\/p>\n<h2><strong>\u52a8\u6001\u5bfc\u5165\u6a21\u5757<\/strong><\/h2>\n<p>module = importlib.import_module(module_name)<\/p>\n<h2><strong>\u83b7\u53d6\u7c7b<\/strong><\/h2>\n<p>MyClass = getattr(module, class_name)<\/p>\n<h2><strong>\u521b\u5efa\u5b9e\u4f8b<\/strong><\/h2>\n<p>my_instance = MyClass(&quot;Alice&quot;)<\/p>\n<p>print(my_instance.greet())  # \u8f93\u51fa: Hello, Alice!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8c03\u7528class\u6587\u4ef6\u6d89\u53ca\u5bfc\u5165\u6a21\u5757\u3001\u5b9e\u4f8b\u5316\u7c7b\u3001\u8c03\u7528\u7c7b\u65b9\u6cd5\u548c\u5c5e\u6027\u7b49\u6b65\u9aa4\u3002\u901a\u8fc7\u4f7f\u7528import\u8bed\u53e5\u548cfrom&#8230;import\u8bed\u53e5\uff0c\u53ef\u4ee5\u5c06class\u6587\u4ef6\u5f15\u5165\u5230\u5f53\u524d\u811a\u672c\u4e2d\u3002\u5b9e\u4f8b\u5316\u7c7b\u662f\u521b\u5efa\u7c7b\u7684\u5bf9\u8c61\uff0c\u901a\u8fc7\u70b9\u64cd\u4f5c\u7b26\u53ef\u4ee5\u8bbf\u95ee\u7c7b\u4e2d\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5305\u548c\u6a21\u5757\u7ec4\u7ec7\u4ee3\u7801\uff0c\u5e76\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u548c\u7edd\u5bf9\u5bfc\u5165\u8fdb\u884c\u8c03\u7528\u3002\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982importlib\u53ef\u4ee5\u5b9e\u73b0\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u793a\u4f8b\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5728Python\u4e2d\u8c03\u7528class\u6587\u4ef6\uff0c\u5e76\u7ec4\u7ec7\u548c\u7ba1\u7406\u4ee3\u7801\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165\u81ea\u5b9a\u4e49\u7c7b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u81ea\u5b9a\u4e49\u7684\u7c7b\u6587\u4ef6\u3002\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u540d\u4e3a<code>my_class.py<\/code>\u7684\u6587\u4ef6\uff0c\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>MyClass<\/code>\u7684\u7c7b\uff0c\u60a8\u53ef\u4ee5\u5728\u53e6\u4e00\u4e2aPython\u811a\u672c\u4e2d\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5bfc\u5165\u5b83\uff1a  <\/p>\n<pre><code class=\"language-python\">from my_class import MyClass\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u521b\u5efa<code>MyClass<\/code>\u7684\u5b9e\u4f8b\u5e76\u4f7f\u7528\u5176\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u7c7b\u6587\u4ef6\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\uff1f<\/strong><br \/>\u786e\u4fdd\u8981\u5bfc\u5165\u7684\u7c7b\u6587\u4ef6\u4e0e\u60a8\u6b63\u5728\u8fd0\u884c\u7684\u811a\u672c\u4f4d\u4e8e\u540c\u4e00\u76ee\u5f55\u4e0b\uff0c\u6216\u8005\u5c06\u7c7b\u6587\u4ef6\u653e\u7f6e\u5728Python\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\u4e2d\u3002\u5982\u679c\u7c7b\u6587\u4ef6\u5728\u4e0d\u540c\u7684\u6587\u4ef6\u5939\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0<code>sys.path<\/code>\u6765\u4fee\u6539\u641c\u7d22\u8def\u5f84\uff0c\u6216\u4f7f\u7528\u5305\u7ed3\u6784\u8fdb\u884c\u7ec4\u7ec7\u3002<\/p>\n<p><strong>\u8c03\u7528\u7c7b\u65f6\u9700\u8981\u6ce8\u610f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u8c03\u7528\u7c7b\u65f6\uff0c\u786e\u4fdd\u60a8\u5df2\u6b63\u786e\u5b9e\u4f8b\u5316\u8be5\u7c7b\u5e76\u4f20\u9012\u4e86\u5fc5\u8981\u7684\u53c2\u6570\u3002\u5982\u679c\u7c7b\u5b9a\u4e49\u4e86<code>__init__<\/code>\u65b9\u6cd5\uff0c\u60a8\u9700\u8981\u63d0\u4f9b\u6240\u9700\u7684\u53c2\u6570\uff0c\u5426\u5219\u4f1a\u5f15\u53d1\u9519\u8bef\u3002\u6b64\u5916\uff0c\u4e86\u89e3\u7c7b\u4e2d\u53ef\u7528\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u4e5f\u662f\u81f3\u5173\u91cd\u8981\u7684\uff0c\u8fd9\u6837\u60a8\u624d\u80fd\u6709\u6548\u5730\u5229\u7528\u8be5\u7c7b\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8c03\u7528class\u6587\u4ef6\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u5bfc\u5165\u6a21\u5757\u3001\u5b9e\u4f8b\u5316\u7c7b\u3001\u8c03\u7528\u7c7b\u65b9\u6cd5\u548c\u5c5e\u6027\u3002\u5bfc\u5165\u6a21\u5757\u662f\u6307\u4f7f\u7528impo [&hellip;]","protected":false},"author":3,"featured_media":1155534,"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\/1155526"}],"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=1155526"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1155526\/revisions"}],"predecessor-version":[{"id":1155535,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1155526\/revisions\/1155535"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1155534"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1155526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1155526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1155526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}