{"id":3464,"date":"2025-05-30T15:11:23","date_gmt":"2025-06-04T07:40:22","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3464"},"modified":"2025-06-04T15:11:23","modified_gmt":"2025-06-04T07:40:22","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84init%e5%87%bd%e6%95%b8%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84init%e5%87%bd%e6%95%b8%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 `__init__()` \u51fd\u5f0f\uff1a\u521d\u59cb\u5316\u985e\u5225\u5c6c\u6027\u7684\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, __init__(), \u985e\u5225\u521d\u59cb\u5316, Python \u6559\u5b78, Python \u7a0b\u5f0f\u8a2d\u8a08\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u7684 `__init__()` \u51fd\u5f0f\uff1a\u521d\u59cb\u5316\u985e\u5225\u5c6c\u6027\u7684\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`__init__()` \u51fd\u5f0f\u662f\u4e00\u500b\u7279\u6b8a\u7684\u65b9\u6cd5\uff0c\u7528\u65bc\u985e\u5225\u7684\u521d\u59cb\u5316\u904e\u7a0b\u3002\u7576\u6211\u5011\u5275\u5efa\u4e00\u500b\u985e\u5225\u7684\u5be6\u4f8b\u6642\uff0c`__init__()` \u65b9\u6cd5\u6703\u81ea\u52d5\u88ab\u8abf\u7528\uff0c\u9019\u4f7f\u5f97\u6211\u5011\u80fd\u5920\u70ba\u985e\u5225\u7684\u5c6c\u6027\u8ce6\u4e88\u521d\u59cb\u503c\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u6703\u6df1\u5165\u63a2\u8a0e `__init__()` \u51fd\u5f0f\u7684\u7528\u6cd5\u4ee5\u53ca\u6700\u4f73\u5be6\u8e10\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u4f5c\u7bc4\u4f8b\u3002<\/p>\n<p>## `__init__()` \u51fd\u5f0f\u7684\u57fa\u672c\u7d50\u69cb<\/p>\n<p>`__init__()` \u51fd\u5f0f\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u6578\u91cf\u7684\u53c3\u6578\uff0c\u4f46\u7b2c\u4e00\u500b\u53c3\u6578\u5fc5\u9808\u662f `self`\uff0c\u5b83\u4ee3\u8868\u8a72\u985e\u5225\u7684\u5be6\u4f8b\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 `__init__()` \u51fd\u5f0f\u4f86\u8a2d\u7f6e\u985e\u5225\u5c6c\u6027\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>my_object = MyClass(&#8220;John&#8221;, 30)<\/p>\n<p>print(my_object.name)  # John<br \/>\nprint(my_object.age)   # 30<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba `MyClass` \u7684\u985e\u5225\u3002`__init__()` \u65b9\u6cd5\u63a5\u53d7\u5169\u500b\u53c3\u6578\uff1a`name` \u548c `age`\u3002\u7576\u6211\u5011\u5275\u5efa `MyClass` \u7684\u5be6\u4f8b `my_object` \u6642\uff0c\u9019\u5169\u500b\u53c3\u6578\u7684\u503c\u88ab\u8ce6\u4e88 `self.name` \u548c `self.age` \u5c6c\u6027\u3002<\/p>\n<p>## \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528 `__init__()` \u51fd\u5f0f\u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<\/p>\n<p>1. **\u5fd8\u8a18\u5b9a\u7fa9 `self`**\uff1a`__init__()` \u51fd\u5f0f\u5fc5\u9808\u5305\u542b `self` \u53c3\u6578\uff0c\u5426\u5247\u5c07\u6703\u5f15\u767c\u932f\u8aa4\u3002<br \/>\n2. **\u4f7f\u7528\u672a\u521d\u59cb\u5316\u7684\u5c6c\u6027**\uff1a\u78ba\u4fdd\u5728 `__init__()` \u4e2d\u8a2d\u5b9a\u6240\u6709\u9700\u8981\u7684\u5c6c\u6027\uff0c\u5426\u5247\u5728\u4f7f\u7528\u6642\u6703\u7522\u751f `AttributeError`\u3002<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>`__init__()` \u51fd\u5f0f\u4e0d\u50c5\u53ef\u4ee5\u7528\u4f86\u521d\u59cb\u5316\u5c6c\u6027\uff0c\u9084\u53ef\u4ee5\u57f7\u884c\u5176\u4ed6\u521d\u59cb\u5316\u64cd\u4f5c\uff0c\u6bd4\u5982\u958b\u555f\u6587\u4ef6\u3001\u9023\u63a5\u8cc7\u6599\u5eab\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u66f4\u8907\u96dc\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass User:<br \/>\n    def __init__(self, username, password):<br \/>\n        self.username = username<br \/>\n        self.password = password<br \/>\n        self.is_authenticated = False<\/p>\n<p>    def authenticate(self, password):<br \/>\n        if password == self.password:<br \/>\n            self.is_authenticated = True<br \/>\n            print(f&#8221;User {self.username} authenticated successfully.&#8221;)<br \/>\n        else:<br \/>\n            print(&#8220;Authentication failed.&#8221;)<\/p>\n<p>user = User(&#8220;alice&#8221;, &#8220;securepassword&#8221;)<br \/>\nuser.authenticate(&#8220;securepassword&#8221;)  # User alice authenticated successfully.<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b `User` \u985e\u5225\uff0c\u9664\u4e86\u521d\u59cb\u5316\u5c6c\u6027\u5916\uff0c\u9084\u5305\u542b\u4e00\u500b `authenticate` \u65b9\u6cd5\u4f86\u9a57\u8b49\u7528\u6236\u3002<\/p>\n<p>## \u7d50\u8a9e<\/p>\n<p>`__init__()` \u51fd\u5f0f\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u5b83\u8b93\u6211\u5011\u80fd\u5920\u9748\u6d3b\u4e14\u6709\u6548\u5730\u521d\u59cb\u5316\u985e\u5225\u5c6c\u6027\uff0c\u4e26\u4e14\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002\u6b32\u4e86\u89e3\u66f4\u591a Python \u7684\u4f7f\u7528\u6280\u5de7\uff0c\u8acb\u53c3\u8003 [\u9019\u88e1](https:\/\/vocus.cc) \u7684\u76f8\u95dc\u6559\u5b78\u6587\u3002<\/p>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: `__init__()` \u51fd\u5f0f\u53ef\u4ee5\u6709\u8fd4\u56de\u503c\u55ce\uff1f**<br \/>\nA1: \u4e0d\u53ef\u4ee5\uff0c`__init__()` \u51fd\u5f0f\u4e0d\u61c9\u8a72\u6709\u8fd4\u56de\u503c\uff0c\u56e0\u70ba\u5b83\u7684\u4e3b\u8981\u76ee\u7684\u662f\u521d\u59cb\u5316\u7269\u4ef6\u3002<\/p>\n<p>**Q2: \u53ef\u4ee5\u5728 `__init__()` \u4e2d\u8abf\u7528\u5176\u4ed6\u65b9\u6cd5\u55ce\uff1f**<br \/>\nA2: \u53ef\u4ee5\uff0c\u60a8\u53ef\u4ee5\u5728 `__init__()` \u4e2d\u8abf\u7528\u5176\u4ed6\u65b9\u6cd5\u4f86\u57f7\u884c\u984d\u5916\u7684\u521d\u59cb\u5316\u908f\u8f2f\u3002<\/p>\n<p>**Q3: \u5982\u4f55\u8655\u7406 `__init__()` \u4e2d\u7684\u53ef\u9078\u53c3\u6578\uff1f**<br \/>\nA3: \u60a8\u53ef\u4ee5\u70ba\u53ef\u9078\u53c3\u6578\u8a2d\u5b9a\u9810\u8a2d\u503c\uff0c\u4f8b\u5982 `def __init__(self, name, age=18):`\uff0c\u9019\u6a23\u5728\u5275\u5efa\u5be6\u4f8b\u6642\u53ef\u4ee5\u9078\u64c7\u4e0d\u63d0\u4f9b `age`\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>init()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u7a2e\u7279\u6b8a\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u5728\u985e\u5225\u4e2d\u5b9a\u7fa9\u7269\u4ef6\u7684\u521d\u59cb\u5316\u884c\u70ba\uff0c\u4e26\u4e14\u53ef\u4ee5\u63a5\u53d7\u53c3\u6578\uff0c\u4ee5\u4fbf\u5728\u5efa\u7acb\u7269\u4ef6\u6642\u5c0d\u5176\u9032\u884c\u521d\u59cb\u5316\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-3464","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-TS","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3464","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=3464"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3464\/revisions"}],"predecessor-version":[{"id":3465,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3464\/revisions\/3465"}],"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=3464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}