{"id":960980,"date":"2024-12-27T03:54:36","date_gmt":"2024-12-26T19:54:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/960980.html"},"modified":"2024-12-27T03:54:38","modified_gmt":"2024-12-26T19:54:38","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/960980.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4e49\u6784\u9020\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103329\/2142c392-3b9b-4014-80e3-50e209eb1b19.webp\" alt=\"python\u5982\u4f55\u5b9a\u4e49\u6784\u9020\u51fd\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6784\u9020\u51fd\u6570\u901a\u8fc7<code>__init__<\/code>\u65b9\u6cd5\u5b9a\u4e49\u3001\u7528\u4e8e\u521d\u59cb\u5316\u5bf9\u8c61\u3001\u53ef\u4ee5\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u4f20\u9012\u53c2\u6570\u3001\u901a\u5e38\u7528\u4e8e\u8bbe\u7f6e\u5bf9\u8c61\u5c5e\u6027<\/strong>\u3002\u6784\u9020\u51fd\u6570\u662f\u7c7b\u7684\u4e00\u90e8\u5206\uff0c\u6bcf\u6b21\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b\u65f6\u90fd\u4f1a\u81ea\u52a8\u8c03\u7528\u3002\u4ee5\u4e0b\u662f\u5173\u4e8ePython\u4e2d\u5982\u4f55\u5b9a\u4e49\u548c\u4f7f\u7528\u6784\u9020\u51fd\u6570\u7684\u8be6\u7ec6\u4ecb\u7ecd\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e00\u3001\u6784\u9020\u51fd\u6570\u7684\u5b9a\u4e49\u4e0e\u57fa\u672c\u4f7f\u7528<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0c\u6784\u9020\u51fd\u6570\u662f\u901a\u8fc7<code>__init__<\/code>\u65b9\u6cd5\u6765\u5b9a\u4e49\u7684\u3002<code>__init__<\/code>\u65b9\u6cd5\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u65b9\u6cd5\uff0c\u5f53\u6211\u4eec\u521b\u5efa\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\u65f6\uff0c\u5b83\u4f1a\u88ab\u81ea\u52a8\u8c03\u7528\u3002\u5176\u4e3b\u8981\u4f5c\u7528\u662f\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self, attribute1, attribute2):<\/p>\n<p>        self.attribute1 = attribute1<\/p>\n<p>        self.attribute2 = attribute2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>MyClass<\/code>\u6709\u4e00\u4e2a\u6784\u9020\u51fd\u6570<code>__init__<\/code>\uff0c\u5b83\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570<code>attribute1<\/code>\u548c<code>attribute2<\/code>\u3002\u8fd9\u4e9b\u53c2\u6570\u88ab\u7528\u6765\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u5c5e\u6027<code>self.attribute1<\/code>\u548c<code>self.attribute2<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u521b\u5efa\u5bf9\u8c61\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u51fd\u6570\u4f20\u9012\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">obj = MyClass(&#39;value1&#39;, &#39;value2&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u7684<code>obj<\/code>\u662f<code>MyClass<\/code>\u7684\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5176\u5c5e\u6027<code>attribute1<\/code>\u548c<code>attribute2<\/code>\u88ab\u521d\u59cb\u5316\u4e3a<code>value1<\/code>\u548c<code>value2<\/code>\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u6784\u9020\u51fd\u6570\u7684\u53c2\u6570\u4f20\u9012<\/strong><\/h2>\n<p><p>\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\uff0c\u8fd9\u4f7f\u5f97\u6211\u4eec\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u53ef\u4ee5\u7075\u6d3b\u5730\u4f20\u9012\u53c2\u6570\u6765\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h2>1. \u9ed8\u8ba4\u53c2\u6570<\/h2>\n<\/p>\n<p><p>\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528\u9ed8\u8ba4\u53c2\u6570\uff0c\u8fd9\u6837\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u53ef\u4ee5\u9009\u62e9\u4e0d\u4f20\u9012\u67d0\u4e9b\u53c2\u6570\uff0c\u4f7f\u7528\u9ed8\u8ba4\u503c\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self, attribute1, attribute2=&#39;default&#39;):<\/p>\n<p>        self.attribute1 = attribute1<\/p>\n<p>        self.attribute2 = attribute2<\/p>\n<p>obj1 = MyClass(&#39;value1&#39;)<\/p>\n<p>obj2 = MyClass(&#39;value1&#39;, &#39;value2&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>obj1<\/code>\u7684<code>attribute2<\/code>\u5c5e\u6027\u5c06\u88ab\u521d\u59cb\u5316\u4e3a\u9ed8\u8ba4\u503c<code>&#39;default&#39;<\/code>\uff0c\u800c<code>obj2<\/code>\u7684<code>attribute2<\/code>\u5c5e\u6027\u5c06\u88ab\u521d\u59cb\u5316\u4e3a<code>&#39;value2&#39;<\/code>\u3002<\/p>\n<\/p>\n<p><h2>2. \u53ef\u53d8\u6570\u91cf\u53c2\u6570<\/h2>\n<\/p>\n<p><p>Python\u7684\u6784\u9020\u51fd\u6570\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>*args<\/code>\u548c<code>kwargs<\/code>\u6765\u63a5\u53d7\u53ef\u53d8\u6570\u91cf\u7684\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self, *args, kwargs):<\/p>\n<p>        self.args = args<\/p>\n<p>        self.kwargs = kwargs<\/p>\n<p>obj = MyClass(1, 2, 3, key1=&#39;value1&#39;, key2=&#39;value2&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>obj.args<\/code>\u5c06\u662f\u4e00\u4e2a\u5305\u542b<code>(1, 2, 3)<\/code>\u7684\u5143\u7ec4\uff0c\u800c<code>obj.kwargs<\/code>\u5c06\u662f\u4e00\u4e2a\u5305\u542b<code>{&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/code>\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u6784\u9020\u51fd\u6570\u7684\u9ad8\u7ea7\u4f7f\u7528<\/strong><\/h2>\n<p><p>\u6784\u9020\u51fd\u6570\u4e0d\u4ec5\u4ec5\u7528\u4e8e\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u5c5e\u6027\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728\u5176\u4e2d\u6267\u884c\u5176\u4ed6\u5fc5\u8981\u7684\u521d\u59cb\u5316\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h2>1. \u6570\u636e\u9a8c\u8bc1<\/h2>\n<\/p>\n<p><p>\u6784\u9020\u51fd\u6570\u4e2d\u53ef\u4ee5\u5305\u542b\u6570\u636e\u9a8c\u8bc1\u903b\u8f91\uff0c\u4ee5\u786e\u4fdd\u5bf9\u8c61\u5728\u521b\u5efa\u65f6\u5904\u4e8e\u6709\u6548\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self, attribute1):<\/p>\n<p>        if not isinstance(attribute1, str):<\/p>\n<p>            r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;attribute1 must be a string&quot;)<\/p>\n<p>        self.attribute1 = attribute1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6784\u9020\u51fd\u6570\u68c0\u67e5<code>attribute1<\/code>\u662f\u5426\u4e3a\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u4e0d\u662f\uff0c\u5219\u629b\u51fa\u4e00\u4e2a<code>ValueError<\/code>\u3002<\/p>\n<\/p>\n<p><h2>2. \u8d44\u6e90\u5206\u914d<\/h2>\n<\/p>\n<p><p>\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5206\u914d\u8d44\u6e90\uff0c\u4f8b\u5982\u6253\u5f00\u6587\u4ef6\u3001\u5efa\u7acb\u7f51\u7edc\u8fde\u63a5\u7b49\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u8fd8\u5e94\u8be5\u5b9e\u73b0\u6790\u6784\u51fd\u6570<code>__del__<\/code>\u6765\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class FileHandler:<\/p>\n<p>    def __init__(self, filename):<\/p>\n<p>        self.file = open(filename, &#39;r&#39;)<\/p>\n<p>    def __del__(self):<\/p>\n<p>        self.file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>FileHandler<\/code>\u7684\u6784\u9020\u51fd\u6570\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u800c\u6790\u6784\u51fd\u6570\u5219\u5173\u95ed\u8be5\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u6784\u9020\u51fd\u6570\u7684\u7ee7\u627f\u4e0e\u91cd\u8f7d<\/strong><\/h2>\n<p><p>\u5728\u7ee7\u627f\u4e2d\uff0c\u5b50\u7c7b\u53ef\u4ee5\u91cd\u8f7d\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u4f46\u9700\u8981\u663e\u5f0f\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\u6765\u786e\u4fdd\u7236\u7c7b\u7684\u5c5e\u6027\u88ab\u6b63\u786e\u521d\u59cb\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def __init__(self, attribute1):<\/p>\n<p>        self.attribute1 = attribute1<\/p>\n<p>class Child(Parent):<\/p>\n<p>    def __init__(self, attribute1, attribute2):<\/p>\n<p>        super().__init__(attribute1)<\/p>\n<p>        self.attribute2 = attribute2<\/p>\n<p>obj = Child(&#39;value1&#39;, &#39;value2&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Child<\/code>\u7c7b\u91cd\u8f7d\u4e86\u7236\u7c7b<code>Parent<\/code>\u7684\u6784\u9020\u51fd\u6570\uff0c\u5e76\u4f7f\u7528<code>super().__init__(attribute1)<\/code>\u8c03\u7528\u4e86\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u6784\u9020\u51fd\u6570\u7684\u591a\u6001\u6027<\/strong><\/h2>\n<p><p>\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u591a\u6001\u6027\u5141\u8bb8\u6211\u4eec\u4ee5\u591a\u79cd\u5f62\u5f0f\u8bbf\u95ee\u5bf9\u8c61\u3002\u901a\u8fc7\u91cd\u8f7d\u6784\u9020\u51fd\u6570\uff0cPython\u5141\u8bb8\u7c7b\u7684\u5b9e\u4f8b\u5316\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u8fdb\u884c\u3002<\/p>\n<\/p>\n<p><h2>1. \u57fa\u672c\u591a\u6001\u5b9e\u73b0<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u63d0\u4f9b\u4e0d\u540c\u7684\u53c2\u6570\u7b7e\u540d\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6784\u9020\u51fd\u6570\u7684\u591a\u6001\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self, attribute1, attribute2=None):<\/p>\n<p>        if attribute2 is None:<\/p>\n<p>            self.attribute1 = attribute1<\/p>\n<p>            self.attribute2 = &#39;default&#39;<\/p>\n<p>        else:<\/p>\n<p>            self.attribute1 = attribute1<\/p>\n<p>            self.attribute2 = attribute2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>MyClass<\/code>\u7684\u6784\u9020\u51fd\u6570\u6839\u636e\u53c2\u6570\u7684\u4e0d\u540c\u6765\u521d\u59cb\u5316\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h2>2. \u66f4\u590d\u6742\u7684\u591a\u6001\u6027\u5b9e\u73b0<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u7c7b\u65b9\u6cd5\u548c\u9759\u6001\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u7684\u6784\u9020\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self, attribute1, attribute2):<\/p>\n<p>        self.attribute1 = attribute1<\/p>\n<p>        self.attribute2 = attribute2<\/p>\n<p>    @classmethod<\/p>\n<p>    def from_string(cls, data_string):<\/p>\n<p>        attribute1, attribute2 = data_string.split(&#39;,&#39;)<\/p>\n<p>        return cls(attribute1, attribute2)<\/p>\n<p>obj = MyClass.from_string(&#39;value1,value2&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b\u65b9\u6cd5<code>from_string<\/code>\uff0c\u5b83\u901a\u8fc7\u89e3\u6790\u5b57\u7b26\u4e32\u6765\u521b\u5efa\u5bf9\u8c61\u3002\u8fd9\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u7684\u5bf9\u8c61\u521b\u5efa\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u6784\u9020\u51fd\u6570\u7684\u6700\u4f73\u5b9e\u8df5<\/strong><\/h2>\n<p><p>\u5728\u4f7f\u7528\u6784\u9020\u51fd\u6570\u65f6\uff0c\u6709\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h2>1. \u7b80\u5355\u660e\u4e86\u7684\u53c2\u6570\u5217\u8868<\/h2>\n<\/p>\n<p><p>\u6784\u9020\u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\u5e94\u8be5\u5c3d\u53ef\u80fd\u7b80\u5355\u660e\u4e86\uff0c\u907f\u514d\u4f7f\u7528\u8fc7\u591a\u6216\u4e0d\u5fc5\u8981\u7684\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h2>2. \u4f7f\u7528\u9ed8\u8ba4\u53c2\u6570<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u9ed8\u8ba4\u53c2\u6570\u53ef\u4ee5\u63d0\u9ad8\u6784\u9020\u51fd\u6570\u7684\u7075\u6d3b\u6027\uff0c\u4f7f\u5f97\u5bf9\u8c61\u7684\u521b\u5efa\u66f4\u52a0\u7b80\u4fbf\u3002<\/p>\n<\/p>\n<p><h2>3. \u6e05\u6670\u7684\u521d\u59cb\u5316\u903b\u8f91<\/h2>\n<\/p>\n<p><p>\u6784\u9020\u51fd\u6570\u4e2d\u7684\u521d\u59cb\u5316\u903b\u8f91\u5e94\u8be5\u6e05\u6670\u6613\u61c2\uff0c\u907f\u514d\u590d\u6742\u7684\u903b\u8f91\u6216\u5927\u91cf\u7684\u8d44\u6e90\u5206\u914d\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>4. \u5408\u7406\u7684<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a><\/h2>\n<\/p>\n<p><p>\u5982\u679c\u6784\u9020\u51fd\u6570\u4e2d\u5305\u542b\u8d44\u6e90\u5206\u914d\u64cd\u4f5c\uff0c\u786e\u4fdd\u5728\u6790\u6784\u51fd\u6570\u6216\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4e2d\u91ca\u653e\u8d44\u6e90\uff0c\u4ee5\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u901a\u8fc7\u5bf9\u6784\u9020\u51fd\u6570\u7684\u6df1\u5165\u7406\u89e3\u548c\u5408\u7406\u4f7f\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u51fa\u66f4\u52a0\u7075\u6d3b\u3001\u53ef\u7ef4\u62a4\u548c\u9ad8\u6548\u7684Python\u4ee3\u7801\u3002\u6784\u9020\u51fd\u6570\u662fPython\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u638c\u63e1\u5176\u7528\u6cd5\u5bf9\u7f16\u5199\u9ad8\u8d28\u91cf\u7684Python\u7a0b\u5e8f\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u6784\u9020\u51fd\u6570\u662f\u4ec0\u4e48\uff1f\u5982\u4f55\u4f7f\u7528\u5b83\uff1f<\/strong><br \/>\u6784\u9020\u51fd\u6570\u662fPython\u7c7b\u4e2d\u7684\u7279\u6b8a\u65b9\u6cd5\uff0c\u7528\u4e8e\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u521d\u59cb\u5316\u5b9e\u4f8b\u53d8\u91cf\u3002\u6784\u9020\u51fd\u6570\u7684\u540d\u79f0\u662f<code>__init__<\/code>\uff0c\u5728\u7c7b\u7684\u5b9e\u4f8b\u5316\u8fc7\u7a0b\u4e2d\u81ea\u52a8\u8c03\u7528\u3002\u60a8\u53ef\u4ee5\u5728\u6784\u9020\u51fd\u6570\u4e2d\u5b9a\u4e49\u53c2\u6570\uff0c\u4ee5\u4fbf\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u4f20\u9012\u503c\u5e76\u521d\u59cb\u5316\u5c5e\u6027\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">class Person:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n\nperson1 = Person(&quot;Alice&quot;, 30)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>name<\/code>\u548c<code>age<\/code>\u662f\u6784\u9020\u51fd\u6570\u7684\u53c2\u6570\uff0c<code>person1<\/code>\u5bf9\u8c61\u88ab\u521d\u59cb\u5316\u4e3a<code>Alice<\/code>\u548c<code>30<\/code>\u3002<\/p>\n<p><strong>\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u6709\u9ed8\u8ba4\u53c2\u6570\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u8bbe\u7f6e\u9ed8\u8ba4\u53c2\u6570\uff0c\u8fd9\u610f\u5473\u7740\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u4e0d\u4f20\u9012\u67d0\u4e9b\u53c2\u6570\u3002\u5982\u679c\u672a\u4f20\u9012\u53c2\u6570\uff0c\u5c06\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">class Person:\n    def __init__(self, name, age=25):\n        self.name = name\n        self.age = age\n\nperson1 = Person(&quot;Bob&quot;)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>person1<\/code>\u7684<code>age<\/code>\u5c06\u9ed8\u8ba4\u4e3a25\uff0c\u56e0\u4e3a\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u6ca1\u6709\u63d0\u4f9b\u8be5\u53c2\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u6784\u9020\u51fd\u6570\u4e2d\u8c03\u7528\u5176\u4ed6\u65b9\u6cd5\uff1f<\/strong><br \/>\u6784\u9020\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u521d\u59cb\u5316\u5c5e\u6027\uff0c\u8fd8\u53ef\u4ee5\u8c03\u7528\u7c7b\u4e2d\u7684\u5176\u4ed6\u65b9\u6cd5\u3002\u5728\u6784\u9020\u51fd\u6570\u4e2d\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>self<\/code>\u6765\u8bbf\u95ee\u7c7b\u7684\u5176\u4ed6\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">class Person:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n        self.display_info()\n\n    def display_info(self):\n        print(f&quot;Name: {self.name}, Age: {self.age}&quot;)\n\nperson1 = Person(&quot;Charlie&quot;, 28)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>display_info<\/code>\u65b9\u6cd5\u5728\u6784\u9020\u51fd\u6570\u4e2d\u88ab\u8c03\u7528\uff0c\u7528\u4e8e\u6253\u5370\u5bf9\u8c61\u7684\u4fe1\u606f\uff0c\u786e\u4fdd\u5728\u5bf9\u8c61\u521b\u5efa\u65f6\u5c31\u80fd\u770b\u5230\u8fd9\u4e9b\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6784\u9020\u51fd\u6570\u901a\u8fc7__init__\u65b9\u6cd5\u5b9a\u4e49\u3001\u7528\u4e8e\u521d\u59cb\u5316\u5bf9\u8c61\u3001\u53ef\u4ee5\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u4f20\u9012\u53c2\u6570\u3001\u901a\u5e38\u7528\u4e8e\u8bbe\u7f6e [&hellip;]","protected":false},"author":3,"featured_media":960986,"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\/960980"}],"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=960980"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/960980\/revisions"}],"predecessor-version":[{"id":960987,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/960980\/revisions\/960987"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/960986"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=960980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=960980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=960980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}