{"id":2890,"date":"2025-06-01T17:02:31","date_gmt":"2025-06-04T09:29:31","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2890"},"modified":"2025-06-04T17:02:31","modified_gmt":"2025-06-04T09:29:31","slug":"%e5%88%a9%e7%94%a8python%e7%9a%84init%e5%87%bd%e6%95%b8%e5%89%b5%e5%bb%ba%e9%a1%9e%e7%9a%84%e5%af%a6%e4%be%8b","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e5%88%a9%e7%94%a8python%e7%9a%84init%e5%87%bd%e6%95%b8%e5%89%b5%e5%bb%ba%e9%a1%9e%e7%9a%84%e5%af%a6%e4%be%8b\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 `__init__()` \u65b9\u6cd5\uff1a2025 \u6700\u65b0\u5be6\u8e10\u8207\u7bc4\u4f8b"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, __init__()\"><\/p>\n<h1>Python \u4e2d\u7684 `__init__()` \u65b9\u6cd5<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`__init__()` \u65b9\u6cd5\u662f\u7528\u4f86\u521d\u59cb\u5316\u985e\u7684\u5be6\u4f8b\u7684\u4e00\u7a2e\u7279\u6b8a\u65b9\u6cd5\u3002\u9019\u500b\u65b9\u6cd5\u5728\u5275\u5efa\u5be6\u4f8b\u6642\u6703\u81ea\u52d5\u88ab\u8abf\u7528\uff0c\u5e6b\u52a9\u6211\u5011\u70ba\u5c0d\u8c61\u8a2d\u7f6e\u521d\u59cb\u72c0\u614b\u3002\u4e86\u89e3 `__init__()` \u65b9\u6cd5\u662f\u638c\u63e1\u9762\u5411\u5c0d\u8c61\u7de8\u7a0b\u7684\u95dc\u9375\u4e4b\u4e00\u3002<\/p>\n<h2>`__init__()` \u65b9\u6cd5\u7684\u57fa\u672c\u8a9e\u6cd5<\/h2>\n<p>`__init__()` \u65b9\u6cd5\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p>&#8220;`python<br \/>\ndef __init__(self, [args]):<br \/>\n    # \u521d\u59cb\u5316\u985e\u7684\u5be6\u4f8b<br \/>\n&#8220;`<\/p>\n<p>&#8211; `self`\uff1a\u9019\u662f\u4e00\u500b\u6307\u5411\u7576\u524d\u5be6\u4f8b\u7684\u5f15\u7528\uff0c\u7528\u65bc\u8a2a\u554f\u5be6\u4f8b\u5c6c\u6027\u548c\u65b9\u6cd5\u3002<br \/>\n&#8211; `[args]`\uff1a\u53ef\u9078\u53c3\u6578\uff0c\u7528\u65bc\u50b3\u905e\u521d\u59cb\u5316\u6240\u9700\u7684\u53c3\u6578\u3002<\/p>\n<h2>`__init__()` \u65b9\u6cd5\u7684\u4f7f\u7528\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u5b8c\u6574\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 `__init__()` \u65b9\u6cd5\u4f86\u5275\u5efa\u4e00\u500b\u985e\u7684\u5be6\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Person:<br \/>\n    def __init__(self, name, age):<br \/>\n        self.name = name<br \/>\n        self.age = age<\/p>\n<p># \u5275\u5efa Person \u985e\u7684\u5be6\u4f8b<br \/>\np1 = Person(&#8220;John&#8221;, 36)<\/p>\n<p># \u8f38\u51fa\u5be6\u4f8b\u7684\u5c6c\u6027<br \/>\nprint(p1.name)  # \u8f38\u51fa: John<br \/>\nprint(p1.age)   # \u8f38\u51fa: 36<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba `Person` \u7684\u985e\uff0c\u4e26\u5728\u5176\u4e2d\u5b9a\u7fa9\u4e86\u4e00\u500b `__init__()` \u65b9\u6cd5\uff0c\u8a72\u65b9\u6cd5\u63a5\u53d7 `name` \u548c `age` \u5169\u500b\u53c3\u6578\u3002\u7576\u6211\u5011\u5275\u5efa `Person` \u985e\u7684\u5be6\u4f8b `p1` \u6642\uff0c\u9019\u4e9b\u53c3\u6578\u88ab\u50b3\u905e\u7d66 `__init__()` \u65b9\u6cd5\uff0c\u4ee5\u521d\u59cb\u5316\u8a72\u5be6\u4f8b\u7684\u5c6c\u6027\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `__init__()` \u65b9\u6cd5\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u554f\u984c\uff1a<\/p>\n<p>1. **\u5fd8\u8a18\u4f7f\u7528 `self`**\uff1a\u5728\u65b9\u6cd5\u5167\u90e8\uff0c\u5fc5\u9808\u4f7f\u7528 `self` \u4f86\u5f15\u7528\u5c6c\u6027\u3002<br \/>\n   &#8220;`python<br \/>\n   class Person:<br \/>\n       def __init__(self, name, age):<br \/>\n           name = name  # \u932f\u8aa4\uff0c\u61c9\u4f7f\u7528 self.name = name<br \/>\n   &#8220;`<\/p>\n<p>2. **\u7f3a\u5c11\u5fc5\u8981\u53c3\u6578**\uff1a\u5982\u679c\u5728\u5275\u5efa\u5be6\u4f8b\u6642\u672a\u63d0\u4f9b\u5fc5\u8981\u7684\u53c3\u6578\uff0c\u5c07\u6703\u5f15\u767c `TypeError`\u3002<br \/>\n   &#8220;`python<br \/>\n   p2 = Person(&#8220;Alice&#8221;)  # \u932f\u8aa4\uff0c\u7f3a\u5c11 age<br \/>\n   &#8220;`<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`__init__()` \u65b9\u6cd5\u4e0d\u50c5\u53ef\u4ee5\u7528\u65bc\u521d\u59cb\u5316\u57fa\u672c\u5c6c\u6027\uff0c\u9084\u53ef\u4ee5\u7528\u4f86\u521d\u59cb\u5316\u66f4\u8907\u96dc\u7684\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5217\u8868\u6216\u5b57\u5178\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Team:<br \/>\n    def __init__(self, name, members):<br \/>\n        self.name = name<br \/>\n        self.members = members  # \u53ef\u521d\u59cb\u5316\u70ba\u5217\u8868<\/p>\n<p>team = Team(&#8220;Developers&#8221;, [&#8220;Alice&#8221;, &#8220;Bob&#8221;, &#8220;Charlie&#8221;])<br \/>\nprint(team.members)  # \u8f38\u51fa: [&#8216;Alice&#8217;, &#8216;Bob&#8217;, &#8216;Charlie&#8217;]\n&#8220;`<\/p>\n<p>\u9019\u500b\u7bc4\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5728 `__init__()` \u65b9\u6cd5\u4e2d\u521d\u59cb\u5316\u4e00\u500b\u6210\u54e1\u5217\u8868\uff0c\u4ee5\u4fbf\u80fd\u5920\u5728\u5718\u968a\u4e2d\u7ba1\u7406\u591a\u500b\u6210\u54e1\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u7269\u4ef6\u5c0e\u5411\u7de8\u7a0b\uff0c\u60a8\u53ef\u4ee5\u53c3\u8003 [\u9019\u7bc7\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/5f7e3f0b9d3f3e001c75b1b6) \u7372\u5f97\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u6df1\u5165\u63a2\u8a0e\u4e86 Python \u4e2d\u7684 `__init__()` \u65b9\u6cd5\uff0c\u5305\u62ec\u5176\u57fa\u672c\u8a9e\u6cd5\u3001\u4f7f\u7528\u7bc4\u4f8b\u548c\u5e38\u898b\u932f\u8aa4\u6392\u9664\u3002\u638c\u63e1\u9019\u500b\u65b9\u6cd5\u5c07\u5c0d\u60a8\u7406\u89e3 Python \u7684\u7269\u4ef6\u5c0e\u5411\u7de8\u7a0b\u6709\u5f88\u5927\u5e6b\u52a9\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: `__init__()` \u65b9\u6cd5\u53ef\u4ee5\u6709\u9ed8\u8a8d\u53c3\u6578\u55ce\uff1f**<br \/>\nA1: \u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u5728 `__init__()` \u4e2d\u4f7f\u7528\u9ed8\u8a8d\u53c3\u6578\uff0c\u4ee5\u4fbf\u5728\u5275\u5efa\u5be6\u4f8b\u6642\u4e0d\u5fc5\u63d0\u4f9b\u6240\u6709\u53c3\u6578\u3002<\/p>\n<p>**Q2: \u5982\u679c\u6211\u4e0d\u9700\u8981\u50b3\u905e\u4efb\u4f55\u53c3\u6578\uff0c`__init__()` \u65b9\u6cd5\u9084\u9700\u8981\u5b9a\u7fa9\u55ce\uff1f**<br \/>\nA2: \u5982\u679c\u4e0d\u9700\u8981\u9032\u884c\u4efb\u4f55\u521d\u59cb\u5316\uff0c\u60a8\u53ef\u4ee5\u4e0d\u5b9a\u7fa9 `__init__()` \u65b9\u6cd5\uff0cPython \u5c07\u81ea\u52d5\u63d0\u4f9b\u4e00\u500b\u9ed8\u8a8d\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u3002<\/p>\n<p>**Q3: \u5982\u4f55\u5728 `__init__()` \u65b9\u6cd5\u4e2d\u8abf\u7528\u5176\u4ed6\u65b9\u6cd5\uff1f**<br \/>\nA3: \u60a8\u53ef\u4ee5\u76f4\u63a5\u5728 `__init__()` \u65b9\u6cd5\u4e2d\u8abf\u7528\u985e\u7684\u5176\u4ed6\u65b9\u6cd5\uff0c\u53ea\u9700\u4f7f\u7528 `self.method_name()` \u7684\u5f62\u5f0f\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u7684init()\u51fd\u6578\u53ef\u4ee5\u7528\u65bc\u5728\u5275\u5efa\u985e\u7684\u5be6\u4f8b\u6642\u521d\u59cb\u5316\u8a72\u5be6\u4f8b\uff0c\u5b83\u53ef\u4ee5\u63d0\u4f9b\u66f4\u591a\u7684\u7075\u6d3b\u6027\u548c\u63a7\u5236\uff0c\u4ee5\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528init()\u51fd\u6578\u4f86\u5275\u5efa\u985e\u7684\u5be6\u4f8b\uff0c\u4ee5\u53ca\u5b83\u7684\u512a\u9ede\u548c\u7f3a\u9ede\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-2890","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-KC","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2890","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=2890"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2890\/revisions"}],"predecessor-version":[{"id":13014,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2890\/revisions\/13014"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=2890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}