{"id":3144,"date":"2025-06-03T16:46:03","date_gmt":"2025-06-04T08:48:02","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3144"},"modified":"2025-06-04T16:46:03","modified_gmt":"2025-06-04T08:48:02","slug":"python%e7%9a%84init%e5%87%bd%e6%95%b8%ef%bc%9a%e7%b0%a1%e5%96%ae%e6%98%93%e6%87%82%e7%9a%84%e4%bb%8b%e7%b4%b9-2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python%e7%9a%84init%e5%87%bd%e6%95%b8%ef%bc%9a%e7%b0%a1%e5%96%ae%e6%98%93%e6%87%82%e7%9a%84%e4%bb%8b%e7%b4%b9-2\/","title":{"rendered":"\u6df1\u5165\u7406\u89e3 Python \u7684 `__init__()` \u51fd\u6578\uff1a\u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, __init__, \u7269\u4ef6\u5c0e\u5411, Python \u7a0b\u5f0f\u6559\u5b78\"><\/p>\n<h1>\u6df1\u5165\u7406\u89e3 Python \u7684 `__init__()` \u51fd\u6578<\/h1>\n<p>Python \u7684 `__init__()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u7279\u6b8a\u65b9\u6cd5\uff0c\u901a\u5e38\u88ab\u7a31\u70ba\u5efa\u69cb\u5b50\uff08constructor\uff09\uff0c\u7528\u65bc\u5728\u5275\u5efa\u7269\u4ef6\u6642\u521d\u59cb\u5316\u7269\u4ef6\u7684\u5c6c\u6027\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u6df1\u5165\u63a2\u8a0e `__init__()` \u51fd\u6578\u7684\u6700\u65b0\u8a9e\u6cd5\u3001\u4f7f\u7528\u65b9\u6cd5\u53ca\u6700\u4f73\u5be6\u8e10\uff0c\u4e26\u63d0\u4f9b\u5be6\u4f5c\u7bc4\u4f8b\u8207\u932f\u8aa4\u6392\u9664\u6280\u5de7\u3002<\/p>\n<p>## `__init__()` \u51fd\u6578\u7684\u5b9a\u7fa9<\/p>\n<p>`__init__()` \u51fd\u6578\u7684\u5b9a\u7fa9\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\ndef __init__(self, *args, **kwargs):<br \/>\n    &#8220;&#8221;&#8221;<br \/>\n    \u521d\u59cb\u5316\u7269\u4ef6<br \/>\n    &#8220;&#8221;&#8221;<br \/>\n    # \u5c0d\u7269\u4ef6\u9032\u884c\u521d\u59cb\u5316<br \/>\n&#8220;`<\/p>\n<p>\u9019\u500b\u51fd\u6578\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u6578\u91cf\u7684\u4f4d\u7f6e\u53c3\u6578\u548c\u95dc\u9375\u5b57\u53c3\u6578\uff0c\u8b93\u4f60\u9748\u6d3b\u5730\u521d\u59cb\u5316\u7269\u4ef6\u7684\u5c6c\u6027\u3002<\/p>\n<p>## \u5982\u4f55\u4f7f\u7528 `__init__()` \u51fd\u6578<\/p>\n<p>\u4ee5\u4e0b\u662f `__init__()` \u51fd\u6578\u7684\u4f7f\u7528\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyClass:<br \/>\n    def __init__(self, name, age):<br \/>\n        self.name = name<br \/>\n        self.age = age<\/p>\n<p>    def display_info(self):<br \/>\n        print(f&#8221;Name: {self.name}, Age: {self.age}&#8221;)<\/p>\n<p># \u5275\u5efa\u4e00\u500b MyClass \u7684\u5be6\u4f8b<br \/>\nperson = MyClass(&#8220;Alice&#8221;, 30)<br \/>\nperson.display_info()<br \/>\n&#8220;`<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b `MyClass` \u985e\u5225\uff0c\u4e26\u4e14\u5728 `__init__()` \u51fd\u6578\u4e2d\u521d\u59cb\u5316\u4e86 `name` \u548c `age` \u5c6c\u6027\u3002\u7576\u6211\u5011\u5275\u5efa `MyClass` \u7684\u5be6\u4f8b\u6642\uff0c\u9019\u4e9b\u5c6c\u6027\u6703\u88ab\u81ea\u52d5\u8a2d\u7f6e\u3002<\/p>\n<p>## `__init__()` \u51fd\u6578\u7684\u512a\u7f3a\u9ede<\/p>\n<p>### \u512a\u9ede<br \/>\n&#8211; \u63d0\u4f9b\u4e86\u7269\u4ef6\u521d\u59cb\u5316\u7684\u9748\u6d3b\u6027\uff0c\u8b93\u4f60\u53ef\u4ee5\u5728\u5275\u5efa\u7269\u4ef6\u7684\u540c\u6642\u8a2d\u7f6e\u5c6c\u6027\u503c\u3002<br \/>\n&#8211; \u53ef\u4ee5\u63a5\u53d7\u591a\u7a2e\u578b\u5225\u7684\u53c3\u6578\uff0c\u4e26\u9032\u884c\u76f8\u61c9\u7684\u521d\u59cb\u5316\u3002<\/p>\n<p>### \u7f3a\u9ede<br \/>\n&#8211; \u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u904e\u591a\u7684\u53c3\u6578\u53ef\u80fd\u6703\u4f7f\u7a0b\u5f0f\u78bc\u8b8a\u5f97\u8907\u96dc\u3002<br \/>\n&#8211; \u4e0d\u7576\u7684\u4f7f\u7528\u53ef\u80fd\u6703\u5c0e\u81f4\u53ef\u8b80\u6027\u4e0b\u964d\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u8655\u7406\u5927\u91cf\u521d\u59cb\u5316\u908f\u8f2f\u6642\u3002<\/p>\n<p>## \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528 `__init__()` \u51fd\u6578\u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<br \/>\n&#8211; \u5fd8\u8a18\u5728\u985e\u5225\u4e2d\u5b9a\u7fa9 `__init__()` \u51fd\u6578\u3002<br \/>\n&#8211; \u932f\u8aa4\u5730\u8abf\u7528 `self.init()` \u800c\u4e0d\u662f `self.__init__()`\uff0c\u9020\u6210\u7121\u6cd5\u521d\u59cb\u5316\u3002<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>&#8211; `__init__()` \u51fd\u6578\u53ef\u4ee5\u642d\u914d\u5176\u4ed6\u7279\u6b8a\u65b9\u6cd5\uff08\u5982 `__str__()` \u548c `__repr__()`\uff09\u4f86\u63d0\u4f9b\u66f4\u8c50\u5bcc\u7684\u7269\u4ef6\u884c\u70ba\u3002<br \/>\n&#8211; \u5728\u8655\u7406\u5927\u578b\u5c08\u6848\u6642\uff0c\u8003\u616e\u4f7f\u7528\u985e\u5225\u7e7c\u627f\u4f86\u7c21\u5316 `__init__()` \u7684\u908f\u8f2f\u3002<\/p>\n<p>## \u7d50\u8ad6<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cPython \u7684 `__init__()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u5728\u5275\u5efa\u7269\u4ef6\u6642\u9032\u884c\u9748\u6d3b\u7684\u521d\u59cb\u5316\u3002\u638c\u63e1 `__init__()` \u7684\u6b63\u78ba\u7528\u6cd5\u5c07\u5927\u5927\u63d0\u5347\u4f60\u7684 Python \u7a0b\u5f0f\u8a2d\u8a08\u80fd\u529b\u3002<\/p>\n<p>\u70ba\u4e86\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u7269\u4ef6\u5c0e\u5411\u7279\u6027\uff0c\u60a8\u53ef\u4ee5\u53c3\u8003 [\u9019\u7bc7\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/6050dbdc6b043e001c9a3c1b) \u4ee5\u7372\u5f97\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1\uff1a`__init__()` \u51fd\u6578\u662f\u5426\u53ef\u4ee5\u8fd4\u56de\u503c\uff1f**<br \/>\nA1\uff1a\u4e0d\u53ef\u4ee5\uff0c`__init__()` \u51fd\u6578\u9ed8\u8a8d\u8fd4\u56de None\u3002<\/p>\n<p>**Q2\uff1a\u80fd\u5426\u5728 `__init__()` \u51fd\u6578\u4e2d\u8abf\u7528\u5176\u4ed6\u65b9\u6cd5\uff1f**<br \/>\nA2\uff1a\u53ef\u4ee5\uff0c\u60a8\u53ef\u4ee5\u5728 `__init__()` \u4e2d\u8abf\u7528\u985e\u5225\u5167\u7684\u5176\u4ed6\u65b9\u6cd5\u4f86\u9032\u884c\u66f4\u8907\u96dc\u7684\u521d\u59cb\u5316\u3002<\/p>\n<p>**Q3\uff1a\u5982\u679c\u6211\u4e0d\u9700\u8981\u81ea\u5b9a\u7fa9\u521d\u59cb\u5316\uff0c\u662f\u5426\u53ef\u4ee5\u7701\u7565 `__init__()` \u51fd\u6578\uff1f**<br \/>\nA3\uff1a\u53ef\u4ee5\uff0cPython \u6703\u81ea\u52d5\u63d0\u4f9b\u4e00\u500b\u9810\u8a2d\u7684 `__init__()` \u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u9078\u64c7\u4e0d\u5b9a\u7fa9\u5b83\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u7684init()\u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5728\u521d\u59cb\u5316\u7269\u4ef6\u6642\u66f4\u52a0\u5bb9\u6613\uff0c\u4e26\u4e14\u66f4\u52a0\u6709\u6548\u7387\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39init()\u51fd\u6578\u7684\u529f\u80fd\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u63d0\u9ad8\u958b\u767c\u6548\u7387\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-3144","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-OI","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3144","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=3144"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3144\/revisions"}],"predecessor-version":[{"id":3145,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3144\/revisions\/3145"}],"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=3144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}