{"id":3340,"date":"2025-06-03T15:22:22","date_gmt":"2025-06-04T08:11:21","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3340"},"modified":"2025-06-04T15:22:22","modified_gmt":"2025-06-04T08:11:21","slug":"python%e4%b8%ad%e7%9a%84init_subclass%e6%96%b9%e6%b3%95-3","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python%e4%b8%ad%e7%9a%84init_subclass%e6%96%b9%e6%b3%95-3\/","title":{"rendered":"\u63a2\u7d22 Python \u4e2d\u7684 `init_subclass()` \u65b9\u6cd5\uff1a2025 \u5e74\u6700\u65b0\u5be6\u4f5c\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, init_subclass, Python \u6559\u5b78, Python \u985e\u5225\"><\/p>\n<h1>\u63a2\u7d22 Python \u4e2d\u7684 `init_subclass()` \u65b9\u6cd5<\/h1>\n<p>\u5728 Python \u7684\u7269\u4ef6\u5c0e\u5411\u7de8\u7a0b\u4e2d\uff0c`init_subclass()` \u65b9\u6cd5\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u5b83\u4f7f\u958b\u767c\u8005\u80fd\u5920\u5728\u5b50\u985e\u5225\u4e2d\u5b9a\u7fa9\u7279\u5b9a\u7684\u884c\u70ba\uff0c\u800c\u7121\u9700\u5f71\u97ff\u7236\u985e\u5225\u7684\u884c\u70ba\u3002\u96a8\u8457 Python \u8a9e\u6cd5\u7684\u6f14\u9032\uff0c\u4e86\u89e3 `init_subclass()` \u7684\u6700\u65b0\u5be6\u4f5c\u53ca\u5176\u6700\u4f73\u5be6\u8e10\u5c0d\u65bc\u958b\u767c\u8005\u4f86\u8aaa\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f `init_subclass()`\uff1f<\/h2>\n<p>`init_subclass()` \u662f Python \u4e2d\u7684\u4e00\u500b\u7279\u6b8a\u65b9\u6cd5\uff0c\u5b83\u5728\u985e\u5225\u88ab\u5275\u5efa\u6642\u81ea\u52d5\u8abf\u7528\u3002\u9019\u610f\u5473\u8457\u6bcf\u7576\u4f60\u5275\u5efa\u4e00\u500b\u5b50\u985e\u5225\u6642\uff0c`init_subclass()` \u5c31\u6703\u88ab\u89f8\u767c\uff0c\u9019\u5c0d\u65bc\u9032\u884c\u985e\u5225\u521d\u59cb\u5316\u6216\u8a3b\u518a\u5b50\u985e\u5225\u7684\u884c\u70ba\u975e\u5e38\u6709\u7528\u3002<\/p>\n<h2>2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u5be6\u4f5c\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f `init_subclass()` \u7684\u57fa\u672c\u7528\u6cd5\u793a\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Parent:<br \/>\n    def __init__(self):<br \/>\n        self.name = &#8216;Parent&#8217;<\/p>\n<p>    @classmethod<br \/>\n    def init_subclass(cls):<br \/>\n        print(f'{cls.__name__} class is initialized&#8217;)<\/p>\n<p>class Child(Parent):<br \/>\n    def __init__(self):<br \/>\n        super().__init__()<br \/>\n        self.name = &#8216;Child&#8217;<\/p>\n<p># \u5275\u5efa Child \u985e\u5225\u7684\u7269\u4ef6<br \/>\nchild_instance = Child()<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b `Parent` \u985e\u5225\u548c\u4e00\u500b `Child` \u985e\u5225\u3002`Child` \u985e\u5225\u7e7c\u627f\u81ea `Parent` \u985e\u5225\uff0c\u4e26\u5728 `Child` \u985e\u5225\u4e2d\u5b9a\u7fa9\u4e86 `init_subclass()` \u65b9\u6cd5\u3002\u7576 `Child` \u985e\u5225\u88ab\u5275\u5efa\u6642\uff0c\u5c07\u6253\u5370\u51fa\u8a72\u985e\u5225\u521d\u59cb\u5316\u7684\u8a0a\u606f\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `init_subclass()` \u65b9\u6cd5\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>1. **\u672a\u5b9a\u7fa9 `init_subclass()` \u65b9\u6cd5**\uff1a\u78ba\u4fdd\u5728\u5b50\u985e\u5225\u4e2d\u6b63\u78ba\u5b9a\u7fa9\u6b64\u65b9\u6cd5\u3002<br \/>\n2. **\u4f7f\u7528\u975e\u985e\u5225\u65b9\u6cd5**\uff1a`init_subclass()` \u5fc5\u9808\u662f\u985e\u5225\u65b9\u6cd5\uff0c\u8acb\u4f7f\u7528 `@classmethod` \u88dd\u98fe\u5668\u3002<br \/>\n3. **\u7236\u985e\u5225\u672a\u6b63\u78ba\u521d\u59cb\u5316**\uff1a\u78ba\u4fdd\u7236\u985e\u5225\u7684\u521d\u59cb\u5316\u904e\u7a0b\u5728\u5b50\u985e\u5225\u4e2d\u6b63\u78ba\u8abf\u7528\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`init_subclass()` \u65b9\u6cd5\u53ef\u7528\u65bc\u591a\u7a2e\u5834\u5408\uff0c\u5982\uff1a<br \/>\n&#8211; \u81ea\u52d5\u8a3b\u518a\u5b50\u985e\u5225\u4ee5\u4fbf\u65bc\u5f8c\u7e8c\u64cd\u4f5c\u3002<br \/>\n&#8211; \u5efa\u7acb API\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u64f4\u5c55\u529f\u80fd\u800c\u7121\u9700\u4fee\u6539\u7236\u985e\u5225\u3002<br \/>\n&#8211; \u5728\u985e\u5225\u4e2d\u5be6\u73fe\u55ae\u4f8b\u6a21\u5f0f\u6216\u5176\u4ed6\u8a2d\u8a08\u6a21\u5f0f\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>`init_subclass()` \u65b9\u6cd5\u662f\u4e00\u500b\u5f37\u5927\u7684\u529f\u80fd\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u5728\u5b50\u985e\u5225\u4e2d\u5b9a\u7fa9\u7279\u5b9a\u884c\u70ba\u800c\u4e0d\u5e72\u64fe\u7236\u985e\u5225\u7684\u884c\u70ba\u3002\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u7a0b\u5f0f\u7684\u53ef\u7dad\u8b77\u6027\uff0c\u4e5f\u4f7f\u5f97\u958b\u767c\u904e\u7a0b\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<p>\u5982\u9700\u66f4\u591a\u95dc\u65bc Python \u985e\u5225\u7684\u6df1\u5165\u6559\u5b78\uff0c\u8acb\u53c3\u8003 [\u9019\u7bc7\u6587\u7ae0](https:\/\/vocus.cc\/article\/634c9e0cfb8b7f001c8f7c6a)\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: `init_subclass()` \u662f\u5426\u53ef\u4ee5\u5728\u7236\u985e\u5225\u4e2d\u91cd\u5beb\uff1f**<br \/>\nA1: \u662f\u7684\uff0c`init_subclass()` \u53ef\u4ee5\u5728\u7236\u985e\u5225\u4e2d\u91cd\u5beb\uff0c\u9019\u6a23\u53ef\u4ee5\u81ea\u5b9a\u7fa9\u6240\u6709\u5b50\u985e\u5225\u5275\u5efa\u6642\u7684\u884c\u70ba\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u5728 `init_subclass()` \u4e2d\u7372\u53d6\u5b50\u985e\u5225\u7684\u5c6c\u6027\uff1f**<br \/>\nA2: \u4f60\u53ef\u4ee5\u4f7f\u7528 `cls` \u53c3\u6578\u4f86\u8a2a\u554f\u5b50\u985e\u5225\u7684\u5c6c\u6027\uff0c\u4f8b\u5982 `cls.some_attribute`\u3002<\/p>\n<p>**Q3: `init_subclass()` \u548c `__init__()` \u6709\u4ec0\u9ebc\u5340\u5225\uff1f**<br \/>\nA3: `init_subclass()` \u662f\u5728\u985e\u5225\u5275\u5efa\u6642\u8abf\u7528\u7684\uff0c\u800c `__init__()` \u662f\u5728\u5be6\u4f8b\u5316\u7269\u4ef6\u6642\u8abf\u7528\u7684\uff0c\u5169\u8005\u7684\u4f5c\u7528\u548c\u4f7f\u7528\u6642\u6a5f\u4e0d\u540c\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>init_subclass()\u662fPython\u4e2d\u4e00\u7a2e\u7279\u6b8a\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u65bc\u5728\u5b50\u985e\u5225\u4e2d\u521d\u59cb\u5316\u7236\u985e\u5225\u7684\u5c6c\u6027\uff0c\u4e26\u63d0\u4f9b\u66f4\u591a\u7684\u7075\u6d3b\u6027\u548c\u63a7\u5236\u3002\u672c\u6587\u5c07\u4ecb\u7d39init_subclass()\u7684\u7528\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u63d0\u9ad8\u7a0b\u5e8f\u7684\u53ef\u64f4\u5c55\u6027\u548c\u53ef\u7dad\u8b77\u6027\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-3340","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-RS","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3340","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=3340"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3340\/revisions"}],"predecessor-version":[{"id":12864,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3340\/revisions\/12864"}],"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=3340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}