{"id":3020,"date":"2025-05-29T16:49:49","date_gmt":"2025-06-04T09:08:49","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3020"},"modified":"2025-06-04T16:49:49","modified_gmt":"2025-06-04T09:08:49","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84isinstance%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%84isinstance%e5%87%bd%e6%95%b8-2\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 isinstance() \u51fd\u6578\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, isinstance(), Python \u7a0b\u5f0f\u6559\u5b78, Python \u985e\u5225\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 isinstance() \u51fd\u6578\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`isinstance()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u6211\u5011\u6aa2\u67e5\u4e00\u500b\u7269\u4ef6\u662f\u5426\u70ba\u67d0\u500b\u985e\u5225\u7684\u5be6\u4f8b\uff0c\u6216\u662f\u5426\u70ba\u5176\u8d85\u985e\u5225\u7684\u5be6\u4f8b\u3002\u9019\u5c0d\u65bc\u9032\u884c\u985e\u578b\u6aa2\u67e5\u3001\u78ba\u4fdd\u7269\u4ef6\u7684\u6b63\u78ba\u6027\u975e\u5e38\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u8a72\u51fd\u6578\u7684\u8a9e\u6cd5\uff1a<\/p>\n<p>&#8220;`python<br \/>\nisinstance(object, classinfo)<br \/>\n&#8220;`<\/p>\n<p>&#8211; `object` \u662f\u8981\u6aa2\u67e5\u7684\u7269\u4ef6\u3002<br \/>\n&#8211; `classinfo` \u662f\u8981\u6aa2\u67e5\u7684\u985e\u5225\u6216\u8d85\u985e\u5225\u3002<\/p>\n<p>\u7576 `object` \u662f `classinfo` \u7684\u5be6\u4f8b\uff0c\u6216\u8005\u662f `classinfo` \u7684\u8d85\u985e\u5225\u7684\u5be6\u4f8b\u6642\uff0c`isinstance()` \u51fd\u6578\u5c07\u6703\u8fd4\u56de `True`\uff0c\u5426\u5247\u8fd4\u56de `False`\u3002<\/p>\n<p>### \u5be6\u4f5c\u7bc4\u4f8b<\/p>\n<p>\u8b93\u6211\u5011\u901a\u904e\u4e00\u500b\u7c21\u55ae\u7684\u4f8b\u5b50\u4f86\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 `isinstance()` \u51fd\u6578\u3002\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u985e\u5225 `Animal`\uff0c\u4ee5\u53ca\u4e00\u500b\u5b50\u985e\u5225 `Dog`\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Animal:<br \/>\n    pass<\/p>\n<p>class Dog(Animal):<br \/>\n    pass<\/p>\n<p>dog = Dog()<\/p>\n<p># \u6aa2\u67e5 dog \u662f\u5426\u70ba Animal \u7684\u5be6\u4f8b<br \/>\nprint(isinstance(dog, Animal))  # True<br \/>\n# \u6aa2\u67e5 dog \u662f\u5426\u70ba Dog \u7684\u5be6\u4f8b<br \/>\nprint(isinstance(dog, Dog))     # True<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u770b\u5230 `dog` \u662f `Animal` \u548c `Dog` \u7684\u5be6\u4f8b\uff0c\u56e0\u6b64 `isinstance()` \u51fd\u6578\u50b3\u56de `True`\u3002<\/p>\n<p>### \u591a\u985e\u5225\u6aa2\u67e5<\/p>\n<p>`isinstance()` \u51fd\u6578\u9084\u53ef\u4ee5\u63a5\u53d7\u591a\u500b\u985e\u5225\u4f5c\u70ba\u53c3\u6578\uff0c\u9019\u5c0d\u65bc\u9700\u8981\u9032\u884c\u591a\u91cd\u985e\u578b\u6aa2\u67e5\u7684\u60c5\u6cc1\u975e\u5e38\u65b9\u4fbf\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nprint(isinstance(dog, (Animal, Dog)))  # True<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u7a2e\u60c5\u6cc1\u4e0b\uff0c`isinstance()` \u51fd\u6578\u6703\u6aa2\u67e5 `dog` \u662f\u5426\u70ba `Animal` \u6216 `Dog` \u7684\u5be6\u4f8b\uff0c\u7531\u65bc `dog` \u662f `Dog` \u7684\u5be6\u4f8b\uff0c\u56e0\u6b64\u8fd4\u56de `True`\u3002<\/p>\n<p>### \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528 `isinstance()` \u51fd\u6578\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u63d0\u793a\u4ee5\u5e6b\u52a9\u4f60\u6392\u9664\u53ef\u80fd\u7684\u932f\u8aa4\uff1a<\/p>\n<p>1. **\u6aa2\u67e5\u985e\u5225\u540d\u7a31**\uff1a\u78ba\u4fdd\u4f60\u6aa2\u67e5\u7684\u985e\u5225\u540d\u7a31\u6b63\u78ba\u7121\u8aa4\u3002<br \/>\n2. **\u6aa2\u67e5\u7e7c\u627f\u7d50\u69cb**\uff1a\u78ba\u8a8d\u7269\u4ef6\u7684\u985e\u5225\u7e7c\u627f\u7d50\u69cb\u6b63\u78ba\uff0c\u5426\u5247\u6703\u5c0e\u81f4 `isinstance()` \u8fd4\u56de\u932f\u8aa4\u7684\u7d50\u679c\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>`isinstance()` \u51fd\u6578\u7684\u61c9\u7528\u975e\u5e38\u5ee3\u6cdb\u3002\u4f60\u53ef\u4ee5\u5728\u958b\u767c API \u6642\u7528\u4f86\u9a57\u8b49\u8acb\u6c42\u7684\u6578\u64da\u985e\u578b\uff0c\u6216\u8005\u5728\u5927\u578b\u61c9\u7528\u4e2d\u9032\u884c\u9748\u6d3b\u7684\u985e\u578b\u6aa2\u67e5\uff0c\u4ee5\u63d0\u5347\u7a0b\u5f0f\u7684\u5065\u58ef\u6027\u3002<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u4e86\u89e3\u66f4\u591a\u95dc\u65bc Python \u985e\u5225\u548c\u7269\u4ef6\u7684\u77e5\u8b58\uff0c\u53ef\u4ee5\u53c3\u8003\u9019\u7bc7 [Python \u985e\u5225\u8207\u7269\u4ef6\u5c0e\u5411\u7a0b\u5f0f\u8a2d\u8a08\u7684\u6559\u5b78](https:\/\/vocus.cc) \u4f86\u6df1\u5165\u5b78\u7fd2\u3002<\/p>\n<p>### \u7e3d\u7d50<\/p>\n<p>\u7e3d\u4e4b\uff0cPython \u4e2d\u7684 `isinstance()` \u51fd\u6578\u662f\u6aa2\u67e5\u7269\u4ef6\u985e\u578b\u7684\u91cd\u8981\u5de5\u5177\u3002\u7121\u8ad6\u662f\u55ae\u4e00\u985e\u5225\u9084\u662f\u591a\u985e\u5225\u6aa2\u67e5\uff0c\u5b83\u90fd\u80fd\u63d0\u4f9b\u6709\u6548\u7684\u652f\u63f4\uff0c\u5e6b\u52a9\u6211\u5011\u78ba\u4fdd\u7a0b\u5f0f\u7684\u6b63\u78ba\u6027\u548c\u5065\u58ef\u6027\u3002<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: `isinstance()` \u51fd\u6578\u662f\u5426\u53ef\u4ee5\u7528\u65bc\u6aa2\u67e5\u81ea\u5b9a\u7fa9\u985e\u5225\uff1f**<br \/>\nA1: \u662f\u7684\uff0c`isinstance()` \u51fd\u6578\u53ef\u4ee5\u7528\u65bc\u6aa2\u67e5\u81ea\u5b9a\u7fa9\u985e\u5225\u7684\u5be6\u4f8b\uff0c\u9019\u5c0d\u65bc\u78ba\u4fdd\u985e\u578b\u6b63\u78ba\u6027\u975e\u5e38\u6709\u5e6b\u52a9\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u4f7f\u7528 `isinstance()` \u9032\u884c\u591a\u500b\u985e\u5225\u7684\u6aa2\u67e5\uff1f**<br \/>\nA2: \u4f60\u53ef\u4ee5\u5c07\u591a\u500b\u985e\u5225\u653e\u5728\u4e00\u500b\u5143\u7d44\u4e2d\u4f5c\u70ba\u7b2c\u4e8c\u500b\u53c3\u6578\u50b3\u905e\u7d66 `isinstance()` \u51fd\u6578\uff0c\u4f8b\u5982 `isinstance(obj, (ClassA, ClassB))`\u3002<\/p>\n<p>**Q3: `isinstance()` \u662f\u5426\u6703\u6aa2\u67e5\u7269\u4ef6\u7684\u6240\u6709\u8d85\u985e\u5225\uff1f**<br \/>\nA3: \u662f\u7684\uff0c`isinstance()` \u51fd\u6578\u6703\u6aa2\u67e5\u7269\u4ef6\u7684\u6240\u6709\u8d85\u985e\u5225\uff0c\u56e0\u6b64\u5982\u679c\u7269\u4ef6\u662f\u67d0\u500b\u8d85\u985e\u5225\u7684\u5be6\u4f8b\uff0c\u4e5f\u6703\u8fd4\u56de `True`\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684isinstance()\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u6aa2\u67e5\u4e00\u500b\u7269\u4ef6\u662f\u5426\u662f\u67d0\u500b\u985e\u5225\u7684\u5be6\u4f8b\uff0c\u53ef\u4ee5\u5e6b\u52a9\u4f60\u66f4\u597d\u5730\u7ba1\u7406\u4f60\u7684\u7a0b\u5f0f\u78bc\u3002\u672c\u6587\u5c07\u4ecb\u7d39isinstance()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5b83\u7684\u512a\u9ede\u548c\u7f3a\u9ede\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-3020","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-MI","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3020","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=3020"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3020\/revisions"}],"predecessor-version":[{"id":3021,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3020\/revisions\/3021"}],"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=3020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}