{"id":1088645,"date":"2025-01-08T13:46:11","date_gmt":"2025-01-08T05:46:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1088645.html"},"modified":"2025-01-08T13:46:13","modified_gmt":"2025-01-08T05:46:13","slug":"python%e7%b1%bb%e4%b8%ad%e6%96%b9%e6%b3%95%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8%e6%96%b9%e6%b3%95%e8%b0%83%e7%94%a8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1088645.html","title":{"rendered":"python\u7c7b\u4e2d\u65b9\u6cd5\u5982\u4f55\u8c03\u7528\u65b9\u6cd5\u8c03\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24201013\/7819bffc-108e-4297-8d49-e5a174ec9cad.webp\" alt=\"python\u7c7b\u4e2d\u65b9\u6cd5\u5982\u4f55\u8c03\u7528\u65b9\u6cd5\u8c03\u7528\" \/><\/p>\n<p><p> \u5728Python\u7c7b\u4e2d\uff0c\u65b9\u6cd5\u8c03\u7528\u65b9\u6cd5\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u7f16\u7a0b\u573a\u666f\u3002<strong>\u65b9\u6cd5\u8c03\u7528\u65b9\u6cd5\u7684\u65b9\u5f0f\u5305\u62ec\uff1a\u76f4\u63a5\u8c03\u7528\u3001\u901a\u8fc7self\u5173\u952e\u5b57\u8c03\u7528\u3001\u901a\u8fc7\u7c7b\u540d\u8c03\u7528\u3001\u901a\u8fc7super()\u8c03\u7528<\/strong>\uff0c\u5176\u4e2d\u6700\u5e38\u89c1\u548c\u6700\u63a8\u8350\u7684\u65b9\u5f0f\u662f\u901a\u8fc7self\u5173\u952e\u5b57\u8c03\u7528\u3002\u8fd9\u79cd\u8c03\u7528\u65b9\u5f0f\u4e0d\u4ec5\u76f4\u89c2\uff0c\u800c\u4e14\u4fbf\u4e8e\u4ee3\u7801\u7684\u7ef4\u62a4\u548c\u6269\u5c55\u3002<strong>\u76f4\u63a5\u8c03\u7528<\/strong>\u610f\u5473\u7740\u5728\u540c\u4e00\u4e2a\u65b9\u6cd5\u4e2d\u76f4\u63a5\u8c03\u7528\u53e6\u4e00\u4e2a\u65b9\u6cd5\uff0c\u800c\u4e0d\u9700\u8981\u4efb\u4f55\u524d\u7f00\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u9759\u6001\u65b9\u6cd5\u548c\u7c7b\u65b9\u6cd5\u3002\u5728\u7c7b\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u7c7b\u540d\u6765\u8c03\u7528\u65b9\u6cd5\u3002\u5728\u7ee7\u627f\u548c\u591a\u6001\u7684\u573a\u666f\u4e0b\uff0c<strong>super()\u8c03\u7528<\/strong>\u662f\u4e00\u4e2a\u5f3a\u6709\u529b\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u76f4\u63a5\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u76f4\u63a5\u8c03\u7528\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u8c03\u7528\u65b9\u5f0f\u3002\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u65b9\u6cd5\u4e2d\u76f4\u63a5\u8c03\u7528\u53e6\u4e00\u4e2a\u65b9\u6cd5\uff0c\u800c\u65e0\u9700\u4f7f\u7528\u4efb\u4f55\u524d\u7f00\u3002\u8fd9\u79cd\u65b9\u5f0f\u901a\u5e38\u9002\u7528\u4e8e\u9759\u6001\u65b9\u6cd5\u548c\u7c7b\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def method1(self):<\/p>\n<p>        print(&quot;Method 1 called&quot;)<\/p>\n<p>        self.method2()<\/p>\n<p>    def method2(self):<\/p>\n<p>        print(&quot;Method 2 called&quot;)<\/p>\n<p>obj = MyClass()<\/p>\n<p>obj.method1()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>method1<\/code>\u76f4\u63a5\u8c03\u7528\u4e86<code>method2<\/code>\uff0c\u5e76\u4e14\u7531\u4e8e\u5b83\u4eec\u90fd\u5728\u540c\u4e00\u4e2a\u7c7b\u4e2d\uff0c\u8c03\u7528\u662f\u76f4\u63a5\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u901a\u8fc7self\u5173\u952e\u5b57\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u7c7b\u7684\u5b9e\u4f8b\u65b9\u6cd5\u4e2d\uff0cself\u5173\u952e\u5b57\u662f\u5fc5\u987b\u7684\u3002<strong>self\u4ee3\u8868\u7c7b\u7684\u5b9e\u4f8b\uff0c\u4f7f\u7528self\u53ef\u4ee5\u8bbf\u95ee\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5<\/strong>\u3002\u8fd9\u4f7f\u5f97\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u90fd\u5f97\u5230\u4e86\u6781\u5927\u7684\u63d0\u5347\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def method1(self):<\/p>\n<p>        print(&quot;Method 1 called&quot;)<\/p>\n<p>        self.method2()<\/p>\n<p>    def method2(self):<\/p>\n<p>        print(&quot;Method 2 called&quot;)<\/p>\n<p>obj = MyClass()<\/p>\n<p>obj.method1()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>method1<\/code>\u4f7f\u7528<code>self.method2()<\/code>\u6765\u8c03\u7528<code>method2<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u53ef\u4ee5\u8c03\u7528\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u8bbf\u95ee\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u7c7b\u540d\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\u4e0b\uff0c\u7279\u522b\u662f\u5f53\u6d89\u53ca\u5230\u7ee7\u627f\u548c\u591a\u6001\u65f6\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u901a\u8fc7\u7c7b\u540d\u6765\u8c03\u7528\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u5f0f\u901a\u5e38\u7528\u4e8e\u7c7b\u65b9\u6cd5\u548c\u9759\u6001\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    @classmethod<\/p>\n<p>    def method1(cls):<\/p>\n<p>        print(&quot;Class Method 1 called&quot;)<\/p>\n<p>        cls.method2()<\/p>\n<p>    @classmethod<\/p>\n<p>    def method2(cls):<\/p>\n<p>        print(&quot;Class Method 2 called&quot;)<\/p>\n<p>MyClass.method1()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>method1<\/code>\u4f7f\u7528<code>cls.method2()<\/code>\u6765\u8c03\u7528<code>method2<\/code>\uff0c\u5176\u4e2d<code>cls<\/code>\u662f\u7c7b\u672c\u8eab\u3002\u8fd9\u79cd\u65b9\u5f0f\u5728\u7c7b\u65b9\u6cd5\u4e2d\u975e\u5e38\u5e38\u89c1\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u901a\u8fc7super()\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u7ee7\u627f\u548c\u591a\u6001\u7684\u573a\u666f\u4e0b\uff0c<strong>super()\u8c03\u7528<\/strong>\u662f\u4e00\u4e2a\u5f3a\u6709\u529b\u7684\u5de5\u5177\u3002\u5b83\u5141\u8bb8\u4f60\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u8fd9\u5bf9\u4e8e\u4ee3\u7801\u7684\u91cd\u7528\u6027\u548c\u6269\u5c55\u6027\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class ParentClass:<\/p>\n<p>    def method1(self):<\/p>\n<p>        print(&quot;Parent Method 1 called&quot;)<\/p>\n<p>class ChildClass(ParentClass):<\/p>\n<p>    def method1(self):<\/p>\n<p>        print(&quot;Child Method 1 called&quot;)<\/p>\n<p>        super().method1()<\/p>\n<p>obj = ChildClass()<\/p>\n<p>obj.method1()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>ChildClass<\/code>\u7684<code>method1<\/code>\u8c03\u7528\u4e86<code>ParentClass<\/code>\u7684<code>method1<\/code>\uff0c\u8fd9\u4f7f\u5f97\u5b50\u7c7b\u53ef\u4ee5\u6269\u5c55\u7236\u7c7b\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5141\u8bb8\u4f60\u5728\u4e0d\u4fee\u6539\u51fd\u6570\u5b9a\u4e49\u7684\u60c5\u51b5\u4e0b\u6269\u5c55\u5176\u529f\u80fd\u3002\u88c5\u9970\u5668\u53ef\u4ee5\u7528\u4e8e\u65b9\u6cd5\u8c03\u7528\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5728\u8c03\u7528\u524d\u540e\u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        print(&quot;Before calling the method&quot;)<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(&quot;After calling the method&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>class MyClass:<\/p>\n<p>    @decorator<\/p>\n<p>    def method1(self):<\/p>\n<p>        print(&quot;Method 1 called&quot;)<\/p>\n<p>obj = MyClass()<\/p>\n<p>obj.method1()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>decorator<\/code>\u5728<code>method1<\/code>\u8c03\u7528\u524d\u540e\u6253\u5370\u4e86\u4e00\u4e9b\u4fe1\u606f\uff0c\u8fd9\u4f7f\u5f97\u65b9\u6cd5\u8c03\u7528\u66f4\u52a0\u7075\u6d3b\u548c\u53ef\u6269\u5c55\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u95ed\u5305<\/h3>\n<\/p>\n<p><p>\u95ed\u5305\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u6355\u83b7\u5e76\u8bb0\u4f4f\u5176\u521b\u5efa\u65f6\u7684\u4e0a\u4e0b\u6587\u73af\u5883\u3002\u5728\u7c7b\u65b9\u6cd5\u8c03\u7528\u4e2d\uff0c\u95ed\u5305\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u52a8\u6001\u65b9\u6cd5\u6216\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<p>    def method1(self):<\/p>\n<p>        def inner_method():<\/p>\n<p>            print(f&quot;Inner method called with value {self.value}&quot;)<\/p>\n<p>        return inner_method<\/p>\n<p>obj = MyClass(10)<\/p>\n<p>inner = obj.method1()<\/p>\n<p>inner()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>method1<\/code>\u8fd4\u56de\u4e86\u4e00\u4e2a\u95ed\u5305<code>inner_method<\/code>\uff0c\u8fd9\u4e2a\u95ed\u5305\u8bb0\u4f4f\u4e86<code>self.value<\/code>\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u53cd\u5c04<\/h3>\n<\/p>\n<p><p>\u53cd\u5c04\u662f\u6307\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u5730\u83b7\u53d6\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002Python\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684<code>getattr<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u53cd\u5c04\uff0c\u8fd9\u5728\u67d0\u4e9b\u52a8\u6001\u573a\u666f\u4e0b\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def method1(self):<\/p>\n<p>        print(&quot;Method 1 called&quot;)<\/p>\n<p>obj = MyClass()<\/p>\n<p>method_name = &#39;method1&#39;<\/p>\n<p>method = getattr(obj, method_name)<\/p>\n<p>method()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>getattr<\/code>\u52a8\u6001\u5730\u83b7\u53d6\u4e86<code>method1<\/code>\u65b9\u6cd5\uff0c\u5e76\u4e14\u6210\u529f\u5730\u8c03\u7528\u4e86\u5b83\u3002\u8fd9\u4f7f\u5f97\u4ee3\u7801\u5728\u5904\u7406\u672a\u77e5\u6216\u52a8\u6001\u65b9\u6cd5\u65f6\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u9ad8\u6027\u80fd\u6216\u5e76\u53d1\u573a\u666f\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5728\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u4e2d\u8c03\u7528\u65b9\u6cd5\u3002Python\u7684<code>threading<\/code>\u548c<code>multiprocessing<\/code>\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>class MyClass:<\/p>\n<p>    def method1(self):<\/p>\n<p>        print(&quot;Method 1 called&quot;)<\/p>\n<p>obj = MyClass()<\/p>\n<p>thread = threading.Thread(target=obj.method1)<\/p>\n<p>thread.start()<\/p>\n<p>thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>method1<\/code>\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u7ebf\u7a0b\u4e2d\u88ab\u8c03\u7528\uff0c\u8fd9\u4f7f\u5f97\u65b9\u6cd5\u8c03\u7528\u66f4\u52a0\u5e76\u53d1\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>\u5f02\u6b65\u7f16\u7a0b\u662f\u4e00\u79cd\u9ad8\u6548\u7684I\/O\u64cd\u4f5c\u65b9\u5f0f\uff0c\u7279\u522b\u9002\u7528\u4e8e\u7f51\u7edc\u8bf7\u6c42\u548c\u6587\u4ef6\u64cd\u4f5c\u3002Python\u7684<code>asyncio<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5f02\u6b65\u7f16\u7a0b\u652f\u6301\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>class MyClass:<\/p>\n<p>    async def method1(self):<\/p>\n<p>        print(&quot;Method 1 called&quot;)<\/p>\n<p>        aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t self.method2()<\/p>\n<p>    async def method2(self):<\/p>\n<p>        print(&quot;Method 2 called&quot;)<\/p>\n<p>obj = MyClass()<\/p>\n<p>asyncio.run(obj.method1())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>method1<\/code>\u548c<code>method2<\/code>\u90fd\u662f\u5f02\u6b65\u65b9\u6cd5\uff0c\u901a\u8fc7<code>await<\/code>\u5173\u952e\u5b57\u5b9e\u73b0\u5f02\u6b65\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u5143\u7c7b<\/h3>\n<\/p>\n<p><p>\u5143\u7c7b\u662f\u521b\u5efa\u7c7b\u7684\u7c7b\uff0c\u5b83\u4eec\u5141\u8bb8\u4f60\u5728\u7c7b\u521b\u5efa\u65f6\u52a8\u6001\u5730\u4fee\u6539\u7c7b\u7684\u884c\u4e3a\u3002\u5143\u7c7b\u53ef\u4ee5\u7528\u4e8e\u65b9\u6cd5\u8c03\u7528\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5728\u7c7b\u521b\u5efa\u65f6\u6ce8\u5165\u6216\u4fee\u6539\u65b9\u6cd5\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Meta(type):<\/p>\n<p>    def __new__(cls, name, bases, dct):<\/p>\n<p>        def method(self):<\/p>\n<p>            print(&quot;Injected method called&quot;)<\/p>\n<p>        dct[&#39;injected_method&#39;] = method<\/p>\n<p>        return super().__new__(cls, name, bases, dct)<\/p>\n<p>class MyClass(metaclass=Meta):<\/p>\n<p>    pass<\/p>\n<p>obj = MyClass()<\/p>\n<p>obj.injected_method()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Meta<\/code>\u5143\u7c7b\u5728\u521b\u5efa<code>MyClass<\/code>\u65f6\u6ce8\u5165\u4e86\u4e00\u4e2a\u65b9\u6cd5<code>injected_method<\/code>\uff0c\u5e76\u4e14\u6210\u529f\u8c03\u7528\u4e86\u5b83\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u591a\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u573a\u666f\u548c\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8c03\u7528\u65b9\u5f0f\u3002\u6bcf\u79cd\u65b9\u5f0f\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u5408\u7406\u4f7f\u7528\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u9ad8\u6548\u3001\u53ef\u8bfb\u548c\u6613\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u7c7b\u4e2d\u5982\u4f55\u5d4c\u5957\u8c03\u7528\u65b9\u6cd5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u7c7b\u7684\u4e00\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u540c\u4e00\u7c7b\u4e2d\u7684\u53e6\u4e00\u4e2a\u65b9\u6cd5\u3002\u53ea\u9700\u5728\u65b9\u6cd5\u5185\u90e8\u4f7f\u7528<code>self.\u65b9\u6cd5\u540d()<\/code>\u7684\u5f62\u5f0f\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u65f6\uff0c\u53ef\u4ee5\u5728\u4e00\u4e2a\u65b9\u6cd5\u4e2d\u8c03\u7528\u53e6\u4e00\u4e2a\u65b9\u6cd5\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u903b\u8f91\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e2d\u8c03\u7528\u5176\u4ed6\u65b9\u6cd5\uff1f<\/strong><br \/>\u6784\u9020\u51fd\u6570<code>__init__<\/code>\u53ef\u4ee5\u7528\u6765\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u5c5e\u6027\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u5728\u5176\u4e2d\u8c03\u7528\u5176\u4ed6\u65b9\u6cd5\u4ee5\u6267\u884c\u989d\u5916\u7684\u8bbe\u7f6e\u6216\u8ba1\u7b97\u3002\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f7f\u7528<code>self.\u65b9\u6cd5\u540d()<\/code>\u6765\u5b9e\u73b0\uff0c\u786e\u4fdd\u5728\u5bf9\u8c61\u521b\u5efa\u65f6\u5c31\u80fd\u5b8c\u6210\u5fc5\u8981\u7684\u521d\u59cb\u5316\u5de5\u4f5c\u3002<\/p>\n<p><strong>\u5982\u679c\u65b9\u6cd5\u9700\u8981\u4f20\u9012\u53c2\u6570\uff0c\u5982\u4f55\u5728\u8c03\u7528\u65f6\u5b9e\u73b0\uff1f<\/strong><br \/>\u5f53\u8c03\u7528\u4e00\u4e2a\u9700\u8981\u53c2\u6570\u7684\u65b9\u6cd5\u65f6\uff0c\u53ef\u4ee5\u5728\u8c03\u7528\u65f6\u4f20\u5165\u76f8\u5e94\u7684\u53c2\u6570\u3002\u5728\u7c7b\u5185\u7684\u65b9\u6cd5\u8c03\u7528\u4e2d\uff0c\u4f9d\u7136\u4f7f\u7528<code>self.\u65b9\u6cd5\u540d(\u53c2\u6570)<\/code>\u7684\u683c\u5f0f\u6765\u5b8c\u6210\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b9\u6cd5\u63a5\u6536\u53c2\u6570\u5e76\u8fd4\u56de\u7ed3\u679c\uff0c\u7136\u540e\u5728\u53e6\u4e00\u4e2a\u65b9\u6cd5\u4e2d\u8c03\u7528\u6b64\u65b9\u6cd5\u5e76\u4f20\u5165\u6240\u9700\u7684\u53c2\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u7c7b\u4e2d\uff0c\u65b9\u6cd5\u8c03\u7528\u65b9\u6cd5\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u7f16\u7a0b\u573a\u666f\u3002\u65b9\u6cd5\u8c03\u7528\u65b9\u6cd5\u7684\u65b9\u5f0f\u5305\u62ec\uff1a\u76f4\u63a5\u8c03\u7528\u3001\u901a\u8fc7self\u5173\u952e\u5b57\u8c03\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1088653,"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\/1088645"}],"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=1088645"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1088645\/revisions"}],"predecessor-version":[{"id":1088654,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1088645\/revisions\/1088654"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1088653"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1088645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1088645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1088645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}