{"id":3258,"date":"2025-05-30T16:24:40","date_gmt":"2025-06-04T08:25:40","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3258"},"modified":"2025-06-04T16:24:40","modified_gmt":"2025-06-04T08:25:40","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84super%e5%87%bd%e6%95%b8-2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84super%e5%87%bd%e6%95%b8-2\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 super() \u51fd\u6578\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, super(), \u7e7c\u627f, \u591a\u91cd\u7e7c\u627f, Python \u6559\u5b78\"><\/p>\n<h1>Python \u4e2d\u7684 super() \u51fd\u6578\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>Python \u4e2d\u7684 super() \u51fd\u6578\u662f\u7528\u65bc\u7ba1\u7406\u985e\u5225\u7e7c\u627f\u7684\u91cd\u8981\u5de5\u5177\uff0c\u5b83\u80fd\u5920\u8b93\u6211\u5011\u5728\u5b50\u985e\u5225\u4e2d\u7e7c\u627f\u7236\u985e\u5225\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u800c\u4e0d\u9700\u8981\u660e\u78ba\u5730\u6307\u5b9a\u7236\u985e\u5225\u540d\u7a31\u3002\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\uff0c\u9084\u6e1b\u5c11\u4e86\u6f5b\u5728\u7684\u932f\u8aa4\uff0c\u7279\u5225\u662f\u5728\u591a\u91cd\u7e7c\u627f\u7684\u60c5\u6cc1\u4e0b\u3002<\/p>\n<p><!--more--><\/p>\n<h2>super() \u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>\u5728 Python \u4e2d\uff0c\u4f7f\u7528 super() \u51fd\u6578\u7684\u57fa\u672c\u65b9\u6cd5\u662f\u5728\u5b50\u985e\u5225\u4e2d\u547c\u53eb\u7236\u985e\u5225\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u57fa\u672c\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Parent:<br \/>\n    def __init__(self):<br \/>\n        print(&#8220;Parent class initialized&#8221;)<\/p>\n<p>class Child(Parent):<br \/>\n    def __init__(self):<br \/>\n        super().__init__()<br \/>\n        print(&#8220;Child class initialized&#8221;)<\/p>\n<p>c = Child()<br \/>\n# Output: Parent class initialized<br \/>\n#         Child class initialized<br \/>\n&#8220;`<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b Parent \u985e\u5225\u548c\u4e00\u500b Child \u985e\u5225\uff0cChild \u985e\u5225\u7e7c\u627f\u81ea Parent \u985e\u5225\u3002\u5728 Child \u985e\u5225\u7684 `__init__()` \u65b9\u6cd5\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 super() \u51fd\u6578\u4f86\u547c\u53eb Parent \u985e\u5225\u7684 `__init__()` \u65b9\u6cd5\uff0c\u4ee5\u78ba\u4fdd Parent \u7684\u521d\u59cb\u5316\u908f\u8f2f\u88ab\u57f7\u884c\u3002<\/p>\n<h2>\u591a\u91cd\u7e7c\u627f\u4e2d\u7684 super() \u7528\u6cd5<\/h2>\n<p>super() \u51fd\u6578\u5728\u591a\u91cd\u7e7c\u627f\u4e2d\u66f4\u986f\u5176\u5a01\u529b\u3002\u4ee5\u4e0b\u7bc4\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5728\u591a\u91cd\u7e7c\u627f\u4e2d\u4f7f\u7528 super()\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Grandparent:<br \/>\n    def __init__(self):<br \/>\n        print(&#8220;Grandparent class initialized&#8221;)<\/p>\n<p>class Parent(Grandparent):<br \/>\n    def __init__(self):<br \/>\n        super().__init__()<br \/>\n        print(&#8220;Parent class initialized&#8221;)<\/p>\n<p>class Child(Parent):<br \/>\n    def __init__(self):<br \/>\n        super().__init__()<br \/>\n        print(&#8220;Child class initialized&#8221;)<\/p>\n<p>c = Child()<br \/>\n# Output: Grandparent class initialized<br \/>\n#         Parent class initialized<br \/>\n#         Child class initialized<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0cChild \u985e\u5225\u900f\u904e super() \u5148\u547c\u53eb Parent \u985e\u5225\u7684 `__init__()` \u65b9\u6cd5\uff0c\u518d\u900f\u904e Parent \u985e\u5225\u7684 super() \u547c\u53eb Grandparent \u985e\u5225\u7684 `__init__()` \u65b9\u6cd5\uff0c\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u6240\u6709\u5c64\u7d1a\u7684\u521d\u59cb\u5316\u908f\u8f2f\u90fd\u88ab\u57f7\u884c\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u7576\u4f7f\u7528 super() \u51fd\u6578\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u932f\u8aa4\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p>1. **TypeError**\uff1a\u5982\u679c\u4f60\u5728 Python 2 \u4e2d\u4f7f\u7528 super()\uff0c\u9700\u8981\u50b3\u5165\u985e\u5225\u548c `self`\u3002\u4f8b\u5982\uff0c`super(Child, self).__init__()`\u3002<br \/>\n2. **Method Resolution Order (MRO)**\uff1a\u5728\u8907\u96dc\u7684\u7e7c\u627f\u7d50\u69cb\u4e2d\uff0c\u78ba\u8a8d MRO \u662f\u5f88\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u4f7f\u7528 `ClassName.__mro__` \u67e5\u770b\u985e\u5225\u7684\u89e3\u6790\u9806\u5e8f\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>super() \u51fd\u6578\u4e0d\u50c5\u9650\u65bc\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u9084\u53ef\u4ee5\u5728\u4efb\u4f55\u65b9\u6cd5\u4e2d\u4f7f\u7528\u3002\u9019\u4f7f\u5f97\u6211\u5011\u5728\u5b50\u985e\u5225\u4e2d\u53ef\u4ee5\u8986\u5beb\u7236\u985e\u5225\u7684\u65b9\u6cd5\uff0c\u4f46\u4ecd\u7136\u80fd\u5920\u8abf\u7528\u7236\u985e\u5225\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Parent:<br \/>\n    def greet(self):<br \/>\n        print(&#8220;Hello from Parent&#8221;)<\/p>\n<p>class Child(Parent):<br \/>\n    def greet(self):<br \/>\n        super().greet()  # \u547c\u53eb\u7236\u985e\u5225\u7684 greet \u65b9\u6cd5<br \/>\n        print(&#8220;Hello from Child&#8221;)<\/p>\n<p>c = Child()<br \/>\nc.greet()<br \/>\n# Output: Hello from Parent<br \/>\n#         Hello from Child<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u7684\u8a2d\u8a08\u4f7f\u5f97\u6211\u5011\u53ef\u4ee5\u5728\u5b50\u985e\u5225\u4e2d\u64f4\u5c55\u7236\u985e\u5225\u7684\u529f\u80fd\uff0c\u800c\u4e0d\u6703\u5b8c\u5168\u8986\u84cb\u5b83\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>super() \u51fd\u6578\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5e6b\u52a9\u6211\u5011\u66f4\u52a0\u6709\u6548\u5730\u5be6\u73fe\u985e\u5225\u7e7c\u627f\u3002\u7121\u8ad6\u662f\u5728\u55ae\u4e00\u7e7c\u627f\u9084\u662f\u591a\u91cd\u7e7c\u627f\u7684\u60c5\u5883\u4e0b\uff0c\u5b83\u90fd\u80fd\u8b93\u6211\u5011\u7684\u7a0b\u5f0f\u78bc\u8b8a\u5f97\u66f4\u52a0\u7c21\u6f54\u4e14\u6613\u65bc\u7dad\u8b77\u3002<\/p>\n<p>\u5982\u679c\u60a8\u60f3\u66f4\u6df1\u5165\u4e86\u89e3 Python \u7684\u7e7c\u627f\u8207\u591a\u91cd\u7e7c\u627f\u6a5f\u5236\uff0c\u8acb\u53c3\u8003\u9019\u7bc7\u6587\u7ae0 [Python \u7e7c\u627f\u8207\u591a\u91cd\u7e7c\u627f\u7684\u5b8c\u6574\u6307\u5357](https:\/\/vocus.cc\/article\/XXXX)\uff08\u8acb\u5c07 XXXX \u66ff\u63db\u70ba\u5be6\u969b\u7684\u9801\u9762ID\uff09\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: super() \u51fd\u6578\u5728 Python 2 \u548c Python 3 \u4e2d\u6709\u4f55\u4e0d\u540c\uff1f**<br \/>\nA: \u5728 Python 2 \u4e2d\uff0c\u4f7f\u7528 super() \u9700\u8981\u50b3\u5165\u985e\u5225\u548c\u5be6\u4f8b\uff1b\u800c\u5728 Python 3 \u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 super()\u3002<\/p>\n<p>**Q2: \u4f7f\u7528 super() \u6642\uff0c\u5982\u4f55\u8655\u7406\u591a\u91cd\u7e7c\u627f\u7684\u885d\u7a81\uff1f**<br \/>\nA: Python \u4f7f\u7528\u65b9\u6cd5\u89e3\u6790\u9806\u5e8f\uff08MRO\uff09\u4f86\u89e3\u6c7a\u591a\u91cd\u7e7c\u627f\u4e2d\u7684\u885d\u7a81\u554f\u984c\uff0c\u53ef\u4ee5\u5229\u7528 `ClassName.__mro__` \u67e5\u8a62\u89e3\u6790\u9806\u5e8f\u3002<\/p>\n<p>**Q3: \u662f\u5426\u53ef\u4ee5\u5728\u975c\u614b\u65b9\u6cd5\u4e2d\u4f7f\u7528 super()\uff1f**<br \/>\nA: \u4e0d\u884c\uff0csuper() \u4e3b\u8981\u7528\u65bc\u5be6\u4f8b\u65b9\u6cd5\u4e2d\uff0c\u975c\u614b\u65b9\u6cd5\u7121\u6cd5\u7372\u53d6\u5be6\u4f8b\u4e0a\u4e0b\u6587\uff0c\u56e0\u6b64\u7121\u6cd5\u4f7f\u7528 super()\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Super()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u8b93\u4f60\u5728\u5b50\u985e\u5225\u4e2d\u7e7c\u627f\u7236\u985e\u5225\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u6539\u7236\u985e\u5225\u7684\u884c\u70ba\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39Python\u4e2d\u7684Super()\u51fd\u6578\uff0c\u4ee5\u53ca\u5b83\u7684\u4f7f\u7528\u65b9\u6cd5\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-3258","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-Qy","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3258","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=3258"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3258\/revisions"}],"predecessor-version":[{"id":12887,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3258\/revisions\/12887"}],"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=3258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}