{"id":3334,"date":"2025-06-02T15:55:40","date_gmt":"2025-06-04T08:11:39","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3334"},"modified":"2025-06-04T15:55:40","modified_gmt":"2025-06-04T08:11:39","slug":"%e4%ba%86%e8%a7%a3python%e7%9a%84%e5%8e%9f%e5%a7%8b%e5%9f%ba%e7%a4%8e%ef%bc%9aorig_bases-2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e7%9a%84%e5%8e%9f%e5%a7%8b%e5%9f%ba%e7%a4%8e%ef%bc%9aorig_bases-2\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684\u539f\u59cb\u57fa\u790e\uff1a\u4f7f\u7528 `orig_bases()` \u51fd\u5f0f\u9032\u884c\u985e\u5225\u7e7c\u627f\u6aa2\u67e5"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, orig_bases, \u985e\u5225\u7e7c\u627f, Python \u6559\u5b78\"><\/p>\n<h1>Python \u4e2d\u7684 `orig_bases()` \u51fd\u5f0f<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`orig_bases()` \u51fd\u5f0f\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u6211\u5011\u53d6\u5f97\u4e00\u500b\u985e\u5225\u7684\u539f\u59cb\u57fa\u5e95\u985e\u5225\uff08\u539f\u578b\uff09\u3002\u9019\u5c0d\u65bc\u6aa2\u67e5\u4e00\u500b\u7269\u4ef6\u662f\u5426\u7e7c\u627f\u81ea\u67d0\u500b\u7279\u5b9a\u7684\u985e\u5225\uff0c\u6216\u8005\u78ba\u8a8d\u4e00\u500b\u985e\u5225\u662f\u5426\u7e7c\u627f\u81ea\u53e6\u4e00\u500b\u985e\u5225\uff0c\u90fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\u3002\u96a8\u8457 Python \u5728 2025 \u5e74\u7684\u6301\u7e8c\u9032\u5316\uff0c\u7406\u89e3\u9019\u500b\u51fd\u5f0f\u7684\u904b\u7528\u5c0d\u65bc\u958b\u767c\u8005\u4f86\u8aaa\u5c24\u5176\u91cd\u8981\u3002<\/p>\n<p>### `orig_bases()` \u7684\u8a9e\u6cd5<\/p>\n<p>`orig_bases()` \u51fd\u5f0f\u7684\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\norig_bases(cls)<br \/>\n&#8220;`<\/p>\n<p>\u5176\u4e2d\uff0c`cls` \u662f\u60a8\u8981\u6aa2\u67e5\u7684\u985e\u5225\u3002\u8a72\u51fd\u5f0f\u5c07\u8fd4\u56de\u4e00\u500b\u5143\u7d44\uff0c\u5305\u542b\u8a72\u985e\u5225\u7684\u539f\u59cb\u57fa\u5e95\u985e\u5225\uff08\u539f\u578b\uff09\u3002<\/p>\n<p>### \u4f7f\u7528\u7bc4\u4f8b<\/p>\n<p>\u8b93\u6211\u5011\u901a\u904e\u5e7e\u500b\u5be6\u4f5c\u7bc4\u4f8b\u4f86\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 `orig_bases()` \u51fd\u5f0f\u3002<\/p>\n<p>#### \u7bc4\u4f8b 1\uff1a\u6aa2\u67e5\u985e\u5225\u7e7c\u627f\u95dc\u4fc2<\/p>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u985e\u5225 `A`\uff0c\u5b83\u7e7c\u627f\u81ea\u985e\u5225 `B`\uff0c\u800c\u985e\u5225 `B` \u53c8\u7e7c\u627f\u81ea\u985e\u5225 `C`\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 `orig_bases()` \u51fd\u5f0f\u4f86\u6aa2\u67e5 `A` \u662f\u5426\u7e7c\u627f\u81ea `C`\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass C:<br \/>\n    pass<\/p>\n<p>class B(C):<br \/>\n    pass<\/p>\n<p>class A(B):<br \/>\n    pass<\/p>\n<p># \u6aa2\u67e5 A \u7684\u57fa\u5e95\u985e\u5225<br \/>\nprint(orig_bases(A))  # \u8f38\u51fa: (<class '__main__.B'>, <class '__main__.C'>)<br \/>\n&#8220;`<\/p>\n<p>\u5982\u679c `A` \u7e7c\u627f\u81ea `C`\uff0c\u5247\u6703\u8fd4\u56de `(<class '__main__.B'>, <class '__main__.C'>)`\uff0c\u9019\u8868\u793a `A` \u7e7c\u627f\u81ea `B` \u548c `C`\u3002<\/p>\n<p>#### \u7bc4\u4f8b 2\uff1a\u6aa2\u67e5\u985e\u5225\u7684\u76f4\u63a5\u57fa\u5e95<\/p>\n<p>\u6211\u5011\u540c\u6a23\u53ef\u4ee5\u6aa2\u67e5\u985e\u5225 `B` \u662f\u5426\u7e7c\u627f\u81ea `A`\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u6aa2\u67e5 B \u7684\u57fa\u5e95\u985e\u5225<br \/>\nprint(orig_bases(B))  # \u8f38\u51fa: (<class '__main__.C'>,)<br \/>\n&#8220;`<\/p>\n<p>\u5982\u679c `B` \u7e7c\u627f\u81ea `A`\uff0c\u5247\u6703\u8fd4\u56de `(<class '__main__.C'>,)`\u3002<\/p>\n<p>### \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528 `orig_bases()` \u6642\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u932f\u8aa4\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8211; **\u985e\u5225\u672a\u5b9a\u7fa9**\uff1a\u78ba\u4fdd\u60a8\u5728\u4f7f\u7528 `orig_bases()` \u524d\u5df2\u7d93\u5b9a\u7fa9\u4e86\u76f8\u95dc\u7684\u985e\u5225\u3002<br \/>\n&#8211; **\u8a9e\u6cd5\u932f\u8aa4**\uff1a\u6aa2\u67e5\u51fd\u5f0f\u7684\u8abf\u7528\u8a9e\u6cd5\u662f\u5426\u6b63\u78ba\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u900f\u904e `orig_bases()` \u51fd\u5f0f\uff0c\u6211\u5011\u53ef\u4ee5\u66f4\u6df1\u5165\u5730\u7406\u89e3\u985e\u5225\u7684\u7e7c\u627f\u7d50\u69cb\uff0c\u9019\u5728\u958b\u767c\u5927\u578b\u61c9\u7528\u7a0b\u5f0f\u6642\u5c24\u70ba\u91cd\u8981\u3002\u5b83\u4e0d\u50c5\u80fd\u5e6b\u52a9\u60a8\u6aa2\u67e5\u985e\u5225\u7e7c\u627f\u95dc\u4fc2\uff0c\u9084\u80fd\u7528\u65bc\u52d5\u614b\u751f\u6210\u985e\u5225\u6216\u9032\u884c\u985e\u5225\u8a2d\u8a08\u3002<\/p>\n<p>\u5c0d\u65bc\u9032\u4e00\u6b65\u5b78\u7fd2 Python \u985e\u5225\u8207\u7e7c\u627f\u7684\u77e5\u8b58\uff0c\u60a8\u53ef\u4ee5\u53c3\u8003 [\u9019\u88e1\u7684\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/605f3d9d3a4a9900568a8e9e)\uff0c\u4ee5\u589e\u5f37\u60a8\u7684\u7406\u89e3\u3002<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: `orig_bases()` \u51fd\u5f0f\u7684\u8fd4\u56de\u503c\u662f\u4ec0\u9ebc\uff1f**<br \/>\nA1: `orig_bases()` \u51fd\u5f0f\u8fd4\u56de\u4e00\u500b\u5143\u7d44\uff0c\u5305\u542b\u6307\u5b9a\u985e\u5225\u7684\u6240\u6709\u76f4\u63a5\u57fa\u5e95\u985e\u5225\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u6aa2\u67e5\u4e00\u500b\u7269\u4ef6\u7684\u985e\u5225\uff1f**<br \/>\nA2: \u60a8\u53ef\u4ee5\u4f7f\u7528 `isinstance()` \u51fd\u5f0f\u4f86\u6aa2\u67e5\u4e00\u500b\u7269\u4ef6\u662f\u5426\u5c6c\u65bc\u67d0\u500b\u7279\u5b9a\u985e\u5225\u3002<\/p>\n<p>**Q3: \u5982\u679c\u6211\u9047\u5230 `TypeError`\uff0c\u8a72\u5982\u4f55\u89e3\u6c7a\uff1f**<br \/>\nA3: `TypeError` \u901a\u5e38\u8868\u793a\u60a8\u50b3\u905e\u7d66 `orig_bases()` \u7684\u53c3\u6578\u4e0d\u662f\u985e\u5225\uff0c\u8acb\u78ba\u4fdd\u60a8\u50b3\u905e\u7684\u662f\u6709\u6548\u7684\u985e\u5225\u540d\u7a31\u3002<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cPython \u4e2d\u7684 `orig_bases()` \u51fd\u5f0f\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u5e6b\u52a9\u6211\u5011\u7406\u89e3\u985e\u5225\u4e4b\u9593\u7684\u7e7c\u627f\u95dc\u4fc2\uff0c\u9032\u800c\u63d0\u5347\u6211\u5011\u7684\u7a0b\u5f0f\u8a2d\u8a08\u80fd\u529b\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u7684\u539f\u59cb\u57fa\u790e\uff0corig_bases()\u51fd\u6578\u53ef\u4ee5\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3Python\u7684\u57fa\u790e\u77e5\u8b58\uff0c\u4e26\u66f4\u6709\u6548\u5730\u4f7f\u7528Python\u7a0b\u5f0f\u8a9e\u8a00\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39orig_bases()\u51fd\u6578\u7684\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u7c21\u55ae\u7684\u793a\u4f8b\u4f86\u89e3\u91cb\u5b83\u7684\u7528\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-3334","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-RM","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3334","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=3334"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3334\/revisions"}],"predecessor-version":[{"id":3335,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3334\/revisions\/3335"}],"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=3334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}