{"id":3194,"date":"2025-05-28T16:35:45","date_gmt":"2025-06-04T08:38:44","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3194"},"modified":"2025-06-04T16:35:45","modified_gmt":"2025-06-04T08:38:44","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84mro%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84mro%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 mro() \u51fd\u6578\uff1a\u591a\u91cd\u7e7c\u627f\u7684\u9806\u5e8f\u6c7a\u5b9a\u8005"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, mro(), \u591a\u91cd\u7e7c\u627f, Python \u6559\u5b78, Python \u985e\u5225\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u7684 mro() \u51fd\u6578<\/h1>\n<p>Python \u662f\u4e00\u7a2e\u591a\u8f09\u3001\u52d5\u614b\u578b\u5225\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5b83\u652f\u63f4\u7269\u4ef6\u5c0e\u5411\u7a0b\u5f0f\u8a2d\u8a08\uff0c\u5176\u4e2d\u6709\u4e00\u500b\u91cd\u8981\u7684\u6982\u5ff5\u5c31\u662f\u591a\u91cd\u7e7c\u627f\uff08Multiple Inheritance\uff09\u3002\u591a\u91cd\u7e7c\u627f\u5141\u8a31\u4e00\u500b\u5b50\u985e\u5225\u540c\u6642\u7e7c\u627f\u591a\u500b\u7236\u985e\u5225\uff0c\u800c mro() \u51fd\u6578\u6b63\u662f\u7528\u4f86\u6c7a\u5b9a\u9019\u4e9b\u7236\u985e\u5225\u7684\u7e7c\u627f\u9806\u5e8f\u3002<\/p>\n<p>## mro() \u65b9\u6cd5\u7684\u57fa\u672c\u6982\u5ff5<\/p>\n<p>mro() \u65b9\u6cd5\u662f Python \u4e2d\u7684\u4e00\u500b\u7279\u6b8a\u65b9\u6cd5\uff0c\u5b83\u7528\u65bc\u6c7a\u5b9a\u591a\u91cd\u7e7c\u627f\u7684\u9806\u5e8f\u3002\u9019\u610f\u5473\u8457\u5b83\u80fd\u5920\u6c7a\u5b9a\u5b50\u985e\u5225\u5728\u7e7c\u627f\u7236\u985e\u5225\u6642\u7684\u9806\u5e8f\u3002mro() \u65b9\u6cd5\u6703\u8fd4\u56de\u4e00\u500b\u5217\u8868\uff0c\u9019\u500b\u5217\u8868\u4e2d\u7684\u5143\u7d20\u5c31\u662f\u5b50\u985e\u5225\u7e7c\u627f\u5404\u7236\u985e\u5225\u7684\u9806\u5e8f\u3002<\/p>\n<p>### mro() \u65b9\u6cd5\u7684\u4f7f\u7528\u7bc4\u4f8b<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 mro() \u65b9\u6cd5\u4f86\u78ba\u5b9a\u985e\u5225\u7684\u7e7c\u627f\u9806\u5e8f\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass A:<br \/>\n    pass<\/p>\n<p>class B(A):<br \/>\n    pass<\/p>\n<p>class C(A):<br \/>\n    pass<\/p>\n<p>class D(B, C):<br \/>\n    pass<\/p>\n<p>print(D.mro())<br \/>\n# \u8f38\u51fa: [<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>]\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u56db\u500b\u985e\u5225\uff0c\u5176\u4e2d D \u985e\u5225\u7e7c\u627f\u4e86 B \u548c C \u5169\u500b\u985e\u5225\u3002\u900f\u904e mro() \u65b9\u6cd5\uff0c\u6211\u5011\u53ef\u4ee5\u6e05\u695a\u5730\u770b\u5230 D \u985e\u5225\u7e7c\u627f\u7684\u9806\u5e8f\u70ba\uff1aD \u2192 B \u2192 C \u2192 A \u2192 object\u3002\u9019\u6a23\u7684\u9806\u5e8f\u80fd\u5920\u5e6b\u52a9\u6211\u5011\u907f\u514d\u5728\u591a\u91cd\u7e7c\u627f\u6642\u51fa\u73fe\u7684\u554f\u984c\uff0c\u4e26\u4f7f\u7a0b\u5f0f\u78bc\u7684\u7d50\u69cb\u66f4\u52a0\u6e05\u6670\u6613\u8b80\u3002<\/p>\n<p>### \u7576\u524d\u7684\u6700\u4f73\u5be6\u8e10<\/p>\n<p>\u5728\u4f7f\u7528 mro() \u65b9\u6cd5\u6642\uff0c\u61c9\u8a72\u9075\u5faa\u4ee5\u4e0b\u6700\u4f73\u5be6\u8e10\uff1a<\/p>\n<p>1. **\u660e\u78ba\u6027**\uff1a\u5728\u5b9a\u7fa9\u985e\u5225\u7684\u6642\u5019\uff0c\u6ce8\u610f\u7e7c\u627f\u7684\u9806\u5e8f\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8907\u96dc\u6027\u3002<br \/>\n2. **\u907f\u514d\u83f1\u5f62\u7e7c\u627f\u554f\u984c**\uff1a\u5728\u591a\u91cd\u7e7c\u627f\u4e2d\uff0c\u78ba\u4fdd\u7236\u985e\u5225\u4e4b\u9593\u7684\u95dc\u4fc2\u6e05\u6670\uff0c\u4ee5\u907f\u514d\u83f1\u5f62\u7e7c\u627f\u554f\u984c\uff0c\u9019\u662f\u591a\u91cd\u7e7c\u627f\u4e2d\u5e38\u898b\u7684\u56f0\u5883\u3002<br \/>\n3. **\u4f7f\u7528 super() \u51fd\u6578**\uff1a\u5728\u985e\u5225\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528 `super()` \u53ef\u4ee5\u78ba\u4fdd\u6309\u7167\u6b63\u78ba\u7684\u7e7c\u627f\u9806\u5e8f\u8abf\u7528\u7236\u985e\u5225\u7684\u65b9\u6cd5\uff0c\u9019\u6a23\u80fd\u5920\u907f\u514d\u76f4\u63a5\u8abf\u7528\u7236\u985e\u5225\u7684\u65b9\u6cd5\u6240\u5c0e\u81f4\u7684\u554f\u984c\u3002<\/p>\n<p>## \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528 mro() \u65b9\u6cd5\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u932f\u8aa4\uff1a<\/p>\n<p>&#8211; **TypeError\uff1a\u4e0d\u80fd\u7e7c\u627f\u81ea\u975e\u985e\u5225**\uff1a\u78ba\u4fdd\u6240\u6709\u7236\u985e\u5225\u90fd\u662f\u6709\u6548\u7684\u985e\u5225\u3002<br \/>\n&#8211; **AttributeError\uff1a\u7269\u4ef6\u6c92\u6709\u5c6c\u6027 mro**\uff1a\u78ba\u4fdd\u8abf\u7528 mro() \u7684\u5c0d\u8c61\u662f\u985e\u5225\uff0c\u800c\u4e0d\u662f\u5be6\u4f8b\u3002<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>mro() \u65b9\u6cd5\u5728\u5927\u578b\u5c08\u6848\u4e2d\u5c24\u70ba\u91cd\u8981\uff0c\u5c24\u5176\u662f\u5728\u6846\u67b6\u958b\u767c\u6216\u9700\u8981\u8907\u96dc\u985e\u5225\u7d50\u69cb\u7684\u60c5\u6cc1\u4e0b\u3002\u4e86\u89e3 mro() \u65b9\u6cd5\u7684\u904b\u4f5c\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u8a2d\u8a08\u51fa\u66f4\u5177\u5f48\u6027\u548c\u53ef\u7dad\u8b77\u6027\u7684\u7a0b\u5f0f\u78bc\u67b6\u69cb\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u7269\u4ef6\u5c0e\u5411\u7279\u6027\uff0c\u5efa\u8b70\u53c3\u8003 [\u9019\u7bc7\u6587\u7ae0](https:\/\/vocus.cc\/article\/5ebd9f3e9d6f813b62e7611f) \u4ee5\u7372\u5f97\u66f4\u591a\u6df1\u5165\u7684\u6559\u5b78\u3002<\/p>\n<p>## \u5e38\u898b\u554f\u984c\u89e3\u7b54\uff08Q&#038;A\uff09<\/p>\n<p>### Q1: mro() \u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u662f\u4ec0\u9ebc\uff1f<br \/>\nA1: mro() \u65b9\u6cd5\u8fd4\u56de\u4e00\u500b\u5217\u8868\uff0c\u986f\u793a\u4e86\u985e\u5225\u7684\u7e7c\u627f\u9806\u5e8f\uff0c\u5f9e\u5b50\u985e\u5225\u5230\u7236\u985e\u5225\u7684\u93c8\u63a5\u3002<\/p>\n<p>### Q2: \u5982\u4f55\u907f\u514d\u591a\u91cd\u7e7c\u627f\u4e2d\u7684\u83f1\u5f62\u554f\u984c\uff1f<br \/>\nA2: \u53ef\u900f\u904e\u660e\u78ba\u7684\u7e7c\u627f\u9806\u5e8f\u548c\u4f7f\u7528 super() \u51fd\u6578\u4f86\u907f\u514d\u83f1\u5f62\u554f\u984c\uff0c\u78ba\u4fdd\u65b9\u6cd5\u8abf\u7528\u7684\u6b63\u78ba\u6027\u3002<\/p>\n<p>### Q3: \u6211\u53ef\u4ee5\u5728 mro() \u65b9\u6cd5\u4e2d\u4f7f\u7528\u81ea\u5b9a\u7fa9\u7684\u6392\u5e8f\u55ce\uff1f<br \/>\nA3: mro() \u65b9\u6cd5\u7684\u8fd4\u56de\u9806\u5e8f\u4f9d\u8cf4\u65bc C3 \u7dda\u6027\u5316\u7b97\u6cd5\uff0c\u4e0d\u652f\u6301\u81ea\u5b9a\u7fa9\u6392\u5e8f\u3002\u5982\u679c\u9700\u8981\u81ea\u5b9a\u7fa9\u884c\u70ba\uff0c\u5efa\u8b70\u91cd\u65b0\u8a2d\u8a08\u985e\u5225\u7d50\u69cb\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MRO()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3Python\u4e2d\u7684\u591a\u91cd\u7e7c\u627f\uff0c\u4e26\u66f4\u6709\u6548\u5730\u4f7f\u7528\u5b83\u5011\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39MRO()\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-3194","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-Pw","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3194","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=3194"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3194\/revisions"}],"predecessor-version":[{"id":3195,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3194\/revisions\/3195"}],"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=3194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}