{"id":3466,"date":"2025-05-29T15:39:28","date_gmt":"2025-06-04T07:39:28","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3466"},"modified":"2025-06-04T15:39:28","modified_gmt":"2025-06-04T07:39:28","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84init_subclass%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_subclass%e5%87%bd%e6%95%b8%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 `init_subclass()` \u51fd\u6578\uff1a2025 \u5e74\u6700\u65b0\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, init_subclass\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u7684 <code>init_subclass()<\/code> \u51fd\u6578\uff1a2025 \u5e74\u6700\u65b0\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`init_subclass()` \u662f\u4e00\u500b\u975e\u5e38\u5f37\u5927\u7684\u51fd\u6578\uff0c\u5141\u8a31\u958b\u767c\u8005\u5728\u5b50\u985e\u5225\u4e2d\u5b9a\u7fa9\u7279\u6b8a\u884c\u70ba\uff0c\u800c\u4e0d\u5f71\u97ff\u7236\u985e\u5225\u7684\u884c\u70ba\u3002\u9019\u4f7f\u5f97\u5b50\u985e\u5225\u53ef\u4ee5\u64c1\u6709\u7368\u7279\u7684\u521d\u59cb\u5316\u908f\u8f2f\uff0c\u589e\u5f37\u4e86\u7269\u4ef6\u5c0e\u5411\u7a0b\u5f0f\u8a2d\u8a08\u7684\u9748\u6d3b\u6027\u3002<\/p>\n<p>### <code>init_subclass()<\/code> \u7684\u4f7f\u7528\u65b9\u6cd5<\/p>\n<p>\u8981\u4f7f\u7528 `init_subclass()` \u51fd\u6578\uff0c\u958b\u767c\u8005\u9996\u5148\u9700\u8981\u5b9a\u7fa9\u4e00\u500b\u7236\u985e\u5225\uff0c\u7136\u5f8c\u5728\u5b50\u985e\u5225\u4e2d\u4f7f\u7528 `init_subclass()` \u51fd\u6578\u4f86\u5b9a\u7fa9\u7279\u6b8a\u884c\u70ba\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\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>class Child(Parent):<br \/>\n    def __init_subclass__(cls):<br \/>\n        super().__init_subclass__()  # \u547c\u53eb\u7236\u985e\u5225\u7684\u521d\u59cb\u5316<br \/>\n        cls.name = &#8216;Child&#8217;           # \u8a2d\u5b9a\u5b50\u985e\u5225\u7684 name \u5c6c\u6027<\/p>\n<p>parent = Parent()<br \/>\nchild = Child()<\/p>\n<p>print(parent.name)  # \u8f38\u51fa: Parent<br \/>\nprint(child.name)   # \u8f38\u51fa: Child<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u7236\u985e\u5225 `Parent`\uff0c\u4e26\u5728\u5b50\u985e\u5225 `Child` \u4e2d\u4f7f\u7528 `init_subclass()` \u51fd\u6578\u4f86\u8a2d\u5b9a\u5b50\u985e\u5225\u7684 `name` \u5c6c\u6027\u70ba &#8216;Child&#8217;\u3002\u9019\u6a23\u505a\u7684\u597d\u8655\u662f\uff0c\u5b50\u985e\u5225\u7684\u884c\u70ba\u6703\u7368\u7acb\u65bc\u7236\u985e\u5225\uff0c\u6e1b\u5c11\u4e86\u8026\u5408\u5ea6\uff0c\u63d0\u9ad8\u4e86\u64f4\u5c55\u6027\u3002<\/p>\n<p>### <code>init_subclass()<\/code> \u7684\u512a\u9ede<\/p>\n<p>1. **\u9748\u6d3b\u6027**\uff1a`init_subclass()` \u5141\u8a31\u958b\u767c\u8005\u5728\u5b50\u985e\u5225\u4e2d\u5b9a\u7fa9\u521d\u59cb\u5316\u908f\u8f2f\uff0c\u9019\u5c0d\u65bc\u9700\u8981\u7279\u6b8a\u8655\u7406\u7684\u5b50\u985e\u5225\u7279\u5225\u6709\u7528\u3002<br \/>\n2. **\u6e1b\u5c11\u91cd\u8907\u4ee3\u78bc**\uff1a\u53ef\u4ee5\u5c07\u901a\u7528\u7684\u521d\u59cb\u5316\u908f\u8f2f\u653e\u5728\u7236\u985e\u5225\u4e2d\uff0c\u53ea\u9700\u8981\u5728\u5b50\u985e\u5225\u4e2d\u8655\u7406\u7279\u5b9a\u7684\u884c\u70ba\uff0c\u907f\u514d\u4ee3\u78bc\u91cd\u8907\u3002<br \/>\n3. **\u589e\u5f37\u53ef\u8b80\u6027**\uff1a\u4f7f\u7528 `init_subclass()` \u53ef\u4ee5\u4f7f\u7a0b\u5f0f\u78bc\u7d50\u69cb\u66f4\u6e05\u6670\uff0c\u8b93\u5176\u4ed6\u958b\u767c\u8005\u66f4\u5bb9\u6613\u7406\u89e3\u985e\u5225\u4e4b\u9593\u7684\u95dc\u4fc2\u3002<\/p>\n<p>### \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u4f7f\u7528 `init_subclass()` \u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>&#8211; **\u672a\u547c\u53eb `super()`**\uff1a\u5982\u679c\u5728 `init_subclass()` \u4e2d\u6c92\u6709\u547c\u53eb `super().__init_subclass__()`\uff0c\u53ef\u80fd\u6703\u5c0e\u81f4\u7236\u985e\u5225\u7684\u521d\u59cb\u5316\u908f\u8f2f\u7121\u6cd5\u57f7\u884c\uff0c\u5f9e\u800c\u5f71\u97ff\u5230\u6574\u500b\u985e\u5225\u7684\u884c\u70ba\u3002<br \/>\n&#8211; **\u932f\u8aa4\u7684\u5c6c\u6027\u8a2d\u5b9a**\uff1a\u5728\u8a2d\u5b9a\u5b50\u985e\u5225\u7684\u5c6c\u6027\u6642\uff0c\u78ba\u4fdd\u4f7f\u7528\u6b63\u78ba\u7684\u5c6c\u6027\u540d\u7a31\uff0c\u4ee5\u907f\u514d\u51fa\u73fe `AttributeError`\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>&#8211; **\u8a3b\u518a\u985e\u5225**\uff1a\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528 `init_subclass()` \u4f86\u81ea\u52d5\u8a3b\u518a\u5b50\u985e\u5225\uff0c\u4f8b\u5982\u5728\u5275\u5efa\u63d2\u4ef6\u7cfb\u7d71\u6216\u6846\u67b6\u6642\u3002<br \/>\n&#8211; **\u81ea\u52d5\u751f\u6210\u5c6c\u6027**\uff1a\u53ef\u4ee5\u6839\u64da\u9700\u8981\u81ea\u52d5\u751f\u6210\u5b50\u985e\u5225\u7684\u5c6c\u6027\uff0c\u4ee5\u7c21\u5316\u985e\u5225\u7684\u8a2d\u8a08\u3002<\/p>\n<p>\u5728 Python \u7684\u7269\u4ef6\u5c0e\u5411\u7a0b\u5f0f\u8a2d\u8a08\u4e2d\uff0c`init_subclass()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\uff0c\u4e86\u89e3\u548c\u638c\u63e1\u5b83\u7684\u4f7f\u7528\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5beb\u51fa\u66f4\u9ad8\u6548\u3001\u53ef\u7dad\u8b77\u7684\u7a0b\u5f0f\u78bc\u3002<\/p>\n<p>\u82e5\u4f60\u60f3\u9032\u4e00\u6b65\u5b78\u7fd2 Python \u7684\u9032\u968e\u7528\u6cd5\uff0c\u53ef\u4ee5\u53c3\u8003 [\u9019\u7bc7\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/605e09b6c0c4c700012b8b8b)\u3002<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: `init_subclass()` \u53ef\u4ee5\u7528\u5728\u54ea\u4e9b\u60c5\u6cc1\u4e0b\uff1f**<br \/>\nA1: \u7576\u9700\u8981\u5728\u5b50\u985e\u5225\u4e2d\u5b9a\u7fa9\u7279\u6b8a\u7684\u521d\u59cb\u5316\u908f\u8f2f\uff0c\u6216\u662f\u7576\u5b50\u985e\u5225\u9700\u8981\u7279\u5b9a\u7684\u5c6c\u6027\u6642\uff0c\u53ef\u4ee5\u4f7f\u7528 `init_subclass()`\u3002<\/p>\n<p>**Q2: \u4f7f\u7528 `init_subclass()` \u662f\u5426\u6703\u5f71\u97ff\u5230\u6027\u80fd\uff1f**<br \/>\nA2: \u5728\u4e00\u822c\u60c5\u6cc1\u4e0b\uff0c`init_subclass()` \u7684\u6027\u80fd\u5f71\u97ff\u662f\u5fae\u4e4e\u5176\u5fae\u7684\uff0c\u56e0\u70ba\u5b83\u4e3b\u8981\u7528\u65bc\u985e\u5225\u7684\u521d\u59cb\u5316\u968e\u6bb5\u3002<\/p>\n<p>**Q3: \u53ef\u4ee5\u5728 `init_subclass()` \u4e2d\u9032\u884c\u5176\u4ed6\u64cd\u4f5c\u55ce\uff1f**<br \/>\nA3: \u662f\u7684\uff0c\u9664\u4e86\u8a2d\u5b9a\u5c6c\u6027\u5916\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5728 `init_subclass()` \u4e2d\u57f7\u884c\u5176\u4ed6\u521d\u59cb\u5316\u908f\u8f2f\uff0c\u4f8b\u5982\u8a3b\u518a\u985e\u5225\u3001\u8a2d\u5b9a\u53c3\u6578\u7b49\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>init_subclass()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u5b50\u985e\u5225\u4e2d\u5b9a\u7fa9\u521d\u59cb\u5316\u51fd\u6578\uff0c\u4ee5\u4fbf\u5728\u5275\u5efa\u5b50\u985e\u5225\u6642\u53ef\u4ee5\u57f7\u884c\u7279\u5b9a\u7684\u64cd\u4f5c\u3002\u9019\u7a2e\u51fd\u6578\u7684\u4f7f\u7528\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5f0f\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\uff0c\u4e26\u6e1b\u5c11\u91cd\u8907\u7684\u7a0b\u5f0f\u78bc\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-3466","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-TU","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3466","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=3466"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3466\/revisions"}],"predecessor-version":[{"id":12823,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3466\/revisions\/12823"}],"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=3466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}