{"id":3326,"date":"2025-05-29T15:26:09","date_gmt":"2025-06-04T08:14:08","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3326"},"modified":"2025-06-04T15:26:09","modified_gmt":"2025-06-04T08:14:08","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84subclasscheck%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%84subclasscheck%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 `issubclass()` \u51fd\u6578\uff1a\u6700\u4f73\u5be6\u8e10\u8207\u7bc4\u4f8b\u6559\u5b78"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, issubclass, subclasscheck, \u7e7c\u627f\u95dc\u4fc2\u6aa2\u67e5\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u7684 `issubclass()` \u51fd\u6578<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`issubclass()` \u51fd\u6578\u662f\u4e00\u500b\u5f37\u5927\u5de5\u5177\uff0c\u7528\u4f86\u6aa2\u67e5\u985e\u5225\u4e4b\u9593\u7684\u7e7c\u627f\u95dc\u4fc2\u3002\u9019\u500b\u51fd\u6578\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u5224\u65b7\u4e00\u500b\u985e\u5225\u662f\u5426\u662f\u53e6\u4e00\u500b\u985e\u5225\u7684\u5b50\u985e\u5225\uff0c\u5c0d\u65bc\u7269\u4ef6\u5c0e\u5411\u7de8\u7a0b\u4f86\u8aaa\uff0c\u9019\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\u3002<\/p>\n<p>\u9019\u7bc7\u6587\u7ae0\u5c07\u6703\u4ecb\u7d39 `issubclass()` \u51fd\u6578\u7684\u6700\u65b0\u8a9e\u6cd5\u3001\u5be6\u4f5c\u7bc4\u4f8b\u3001\u932f\u8aa4\u6392\u9664\u4ee5\u53ca\u5ef6\u4f38\u61c9\u7528\uff0c\u8b93\u4f60\u80fd\u5920\u66f4\u6709\u6548\u7387\u5730\u4f7f\u7528\u9019\u500b\u51fd\u6578\u3002<\/p>\n<p><!--more--><\/p>\n<p>## `issubclass()` \u51fd\u6578\u8a9e\u6cd5<\/p>\n<p>`issubclass()` \u51fd\u6578\u7684\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nissubclass(sub, sup)<br \/>\n&#8220;`<\/p>\n<p>&#8211; **sub**\uff1a\u8981\u6aa2\u67e5\u7684\u5b50\u985e\u5225\u3002<br \/>\n&#8211; **sup**\uff1a\u8981\u6aa2\u67e5\u7684\u7236\u985e\u5225\u3002<\/p>\n<p>\u5982\u679c `sub` \u78ba\u5be6\u662f `sup` \u7684\u5b50\u985e\u5225\uff0c\u5247\u8a72\u51fd\u6578\u6703\u8fd4\u56de `True`\uff0c\u5426\u5247\u8fd4\u56de `False`\u3002<\/p>\n<p>## \u4f7f\u7528\u7bc4\u4f8b<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528 `issubclass()` \u51fd\u6578\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass A:<br \/>\n    pass<\/p>\n<p>class B(A):<br \/>\n    pass<\/p>\n<p>print(issubclass(B, A))  # \u8f38\u51fa: True<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u5169\u500b\u985e\u5225 `A` \u548c `B`\uff0c\u5176\u4e2d `B` \u7e7c\u627f\u81ea `A`\u3002\u57f7\u884c\u7d50\u679c\u986f\u793a `True`\uff0c\u9019\u8b49\u660e\u4e86 `B` \u78ba\u5be6\u662f `A` \u7684\u5b50\u985e\u5225\u3002<\/p>\n<p>## \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u4f7f\u7528 `issubclass()` \u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>1. **TypeError**\uff1a\u5982\u679c\u63d0\u4f9b\u7684\u53c3\u6578\u4e0d\u662f\u985e\u5225\uff0c\u5c07\u6703\u5f15\u767c\u6b64\u932f\u8aa4\u3002\u78ba\u4fdd\u50b3\u5165\u7684 `sub` \u548c `sup` \u53c3\u6578\u90fd\u662f\u985e\u5225\u3002<\/p>\n<p>   &#8220;`python<br \/>\n   print(issubclass(B, &#8220;A&#8221;))  # \u9019\u5c07\u5f15\u767c TypeError<br \/>\n   &#8220;`<\/p>\n<p>2. **\u6aa2\u67e5\u591a\u91cd\u7e7c\u627f**\uff1a\u5728 Python \u4e2d\uff0c\u985e\u5225\u652f\u6301\u591a\u91cd\u7e7c\u627f\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u6aa2\u67e5\u591a\u500b\u7236\u985e\u5225\u7684\u95dc\u4fc2\u3002<\/p>\n<p>   &#8220;`python<br \/>\n   class C(A):<br \/>\n       pass<\/p>\n<p>   print(issubclass(B, (A, C)))  # \u8f38\u51fa: True<br \/>\n   &#8220;`<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>`issubclass()` \u51fd\u6578\u4e0d\u50c5\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u55ae\u4e00\u7236\u985e\u5225\uff0c\u9084\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u591a\u500b\u7236\u985e\u5225\u7684\u95dc\u4fc2\uff0c\u9019\u5c0d\u65bc\u8907\u96dc\u7684\u985e\u5225\u7d50\u69cb\u7279\u5225\u6709\u7528\u3002\u5728\u5927\u578b\u9805\u76ee\u4e2d\uff0c\u5408\u7406\u5229\u7528\u9019\u500b\u51fd\u6578\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002<\/p>\n<p>\u82e5\u60f3\u6df1\u5165\u4e86\u89e3 Python \u985e\u5225\u8207\u7e7c\u627f\u7684\u76f8\u95dc\u6982\u5ff5\uff0c\u8acb\u53c3\u8003 [\u9019\u7bc7\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/6057dcd6fd89780001f3ef93)\u3002<\/p>\n<p>## \u7d50\u8ad6<\/p>\n<p>\u900f\u904e\u672c\u6587\uff0c\u4f60\u61c9\u8a72\u5c0d `issubclass()` \u51fd\u6578\u6709\u4e86\u66f4\u6df1\u523b\u7684\u7406\u89e3\u3002\u7121\u8ad6\u662f\u5728\u6aa2\u67e5\u985e\u5225\u95dc\u4fc2\u9084\u662f\u5728\u8a2d\u8a08\u7269\u4ef6\u5c0e\u5411\u67b6\u69cb\u6642\uff0c\u9019\u500b\u51fd\u6578\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002<\/p>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: `issubclass()` \u53ef\u4ee5\u6aa2\u67e5\u591a\u91cd\u7e7c\u627f\u55ce\uff1f**<br \/>\nA1: \u662f\u7684\uff0c`issubclass()` \u53ef\u4ee5\u6aa2\u67e5\u4e00\u500b\u985e\u5225\u662f\u5426\u662f\u591a\u500b\u7236\u985e\u5225\u7684\u5b50\u985e\u5225\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u8655\u7406 `TypeError` \u932f\u8aa4\uff1f**<br \/>\nA2: \u78ba\u4fdd\u4f60\u50b3\u5165\u7684\u53c3\u6578\u90fd\u662f\u5408\u6cd5\u7684\u985e\u5225\uff0c\u907f\u514d\u50b3\u5165\u5176\u4ed6\u985e\u578b\u7684\u5c0d\u8c61\u3002<\/p>\n<p>**Q3: `issubclass()` \u8207 `isinstance()` \u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f**<br \/>\nA3: `issubclass()` \u7528\u65bc\u6aa2\u67e5\u985e\u5225\u4e4b\u9593\u7684\u7e7c\u627f\u95dc\u4fc2\uff0c\u800c `isinstance()` \u7528\u65bc\u6aa2\u67e5\u4e00\u500b\u5be6\u4f8b\u662f\u5426\u5c6c\u65bc\u67d0\u500b\u985e\u5225\u6216\u5176\u5b50\u985e\u5225\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>subclasscheck()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u6aa2\u67e5\u4e00\u500b\u7269\u4ef6\u662f\u5426\u70ba\u53e6\u4e00\u500b\u7269\u4ef6\u7684\u5b50\u985e\u5225\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39subclasscheck()\u51fd\u6578\u7684\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u793a\u4f8b\u4f86\u8aaa\u660e\u5982\u4f55\u4f7f\u7528\u5b83\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-3326","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-RE","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3326","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=3326"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3326\/revisions"}],"predecessor-version":[{"id":3327,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3326\/revisions\/3327"}],"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=3326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}