{"id":3324,"date":"2025-06-01T15:45:41","date_gmt":"2025-06-04T08:13:41","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3324"},"modified":"2025-06-04T15:45:41","modified_gmt":"2025-06-04T08:13:41","slug":"python%e4%b8%ad%e7%9a%84instancecheck%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python%e4%b8%ad%e7%9a%84instancecheck%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 `isinstance()` \u51fd\u6578\uff1a2025 \u6700\u65b0\u7528\u6cd5\u8207\u6280\u5de7"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, isinstance, instancecheck\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u7684 `isinstance()` \u51fd\u6578<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`isinstance()` \u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u51fd\u5f0f\uff0c\u5b83\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u4e00\u500b\u7269\u4ef6\u662f\u5426\u70ba\u67d0\u500b\u985e\u5225\u7684\u5be6\u4f8b\u3002\u9019\u500b\u51fd\u5f0f\u4e0d\u50c5\u53ef\u4ee5\u6aa2\u67e5\u7269\u4ef6\u662f\u5426\u70ba\u67d0\u500b\u985e\u5225\u7684\u5be6\u4f8b\uff0c\u9084\u53ef\u4ee5\u6aa2\u67e5\u5176\u662f\u5426\u70ba\u8a72\u985e\u5225\u7684\u5b50\u985e\u5225\u6216\u7236\u985e\u5225\u7684\u5be6\u4f8b\u3002\u9019\u4f7f\u5f97 `isinstance()` \u6210\u70ba Python \u958b\u767c\u8005\u65e5\u5e38\u7de8\u7a0b\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<h2>`isinstance()` \u7684\u4f7f\u7528\u65b9\u6cd5<\/h2>\n<p>`isinstance()` \u7684\u4f7f\u7528\u65b9\u6cd5\u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u8981\u50b3\u5165\u5169\u500b\u53c3\u6578\uff1a\u7b2c\u4e00\u500b\u53c3\u6578\u662f\u8981\u6aa2\u67e5\u7684\u7269\u4ef6\uff0c\u7b2c\u4e8c\u500b\u53c3\u6578\u662f\u8981\u6aa2\u67e5\u7684\u985e\u5225\u6216\u985e\u5225\u5143\u7d44\u3002\u5982\u679c\u50b3\u5165\u7684\u7269\u4ef6\u662f\u8a72\u985e\u5225\u7684\u5be6\u4f8b\uff0c\u5247\u6703\u56de\u50b3 `True`\uff0c\u5426\u5247\u6703\u56de\u50b3 `False`\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\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>a = A()<br \/>\nb = B()<\/p>\n<p>print(isinstance(a, A))  # True<br \/>\nprint(isinstance(b, A))  # True<br \/>\nprint(isinstance(b, B))  # True<br \/>\nprint(isinstance(a, B))  # False<br \/>\n&#8220;`<\/p>\n<h2>\u4f7f\u7528 `isinstance()` \u7684\u512a\u52e2<\/h2>\n<p>`isinstance()` \u7684\u512a\u52e2\u5728\u65bc\u5b83\u8b93\u6211\u5011\u80fd\u5920\u65b9\u4fbf\u5730\u6aa2\u67e5\u7269\u4ef6\u7684\u578b\u5225\uff0c\u800c\u4e0d\u9700\u8981\u624b\u52d5\u6aa2\u67e5\u7269\u4ef6\u7684\u5c6c\u6027\u6216\u65b9\u6cd5\u3002\u6b64\u5916\uff0c\u4f7f\u7528 `isinstance()` \u9084\u80fd\u5920\u907f\u514d\u8a31\u591a\u6f5b\u5728\u7684\u932f\u8aa4\uff0c\u4f8b\u5982\u5728\u8a2a\u554f\u5c6c\u6027\u4e4b\u524d\u78ba\u8a8d\u7269\u4ef6\u7684\u578b\u5225\u3002<\/p>\n<h2>\u4f7f\u7528 `isinstance()` \u7684\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5118\u7ba1 `isinstance()` \u5177\u6709\u8a31\u591a\u512a\u9ede\uff0c\u4f46\u4e5f\u6709\u4e00\u4e9b\u9700\u8981\u6ce8\u610f\u7684\u5730\u65b9\uff1a<\/p>\n<p>1. **\u6027\u80fd\u8003\u91cf**\uff1a\u5728\u6027\u80fd\u654f\u611f\u7684\u74b0\u5883\u4e2d\uff0c\u904e\u5ea6\u4f7f\u7528 `isinstance()` \u53ef\u80fd\u6703\u5c0e\u81f4\u6027\u80fd\u4e0b\u964d\uff0c\u7279\u5225\u662f\u5728\u5927\u578b\u6578\u64da\u96c6\u4e0a\u8fed\u4ee3\u6642\u3002<br \/>\n2. **\u591a\u91cd\u7e7c\u627f**\uff1a\u5728\u4f7f\u7528\u591a\u91cd\u7e7c\u627f\u6642\uff0c\u6aa2\u67e5\u7269\u4ef6\u7684\u578b\u5225\u53ef\u80fd\u6703\u8b8a\u5f97\u8907\u96dc\uff0c\u9019\u6642\u9700\u8981\u5c0f\u5fc3\u4f7f\u7528\u3002<\/p>\n<h2>`isinstance()` \u7684\u61c9\u7528\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u5e7e\u500b `isinstance()` \u7684\u61c9\u7528\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u6709\u6548\u6aa2\u67e5\u7269\u4ef6\u7684\u985e\u578b\uff1a<\/p>\n<p>1. \u6aa2\u67e5\u7269\u4ef6\u662f\u5426\u70ba\u5b57\u4e32\u985e\u578b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nif isinstance(obj, str):<br \/>\n    print(&#8220;\u9019\u662f\u4e00\u500b\u5b57\u4e32&#8221;)<br \/>\n&#8220;`<\/p>\n<p>2. \u6aa2\u67e5\u7269\u4ef6\u662f\u5426\u70ba\u6578\u503c\u985e\u578b\uff08\u6574\u6578\u6216\u6d6e\u9ede\u6578\uff09\uff1a<\/p>\n<p>&#8220;`python<br \/>\nif isinstance(obj, (int, float)):<br \/>\n    print(&#8220;\u9019\u662f\u4e00\u500b\u6578\u503c&#8221;)<br \/>\n&#8220;`<\/p>\n<p>3. \u6aa2\u67e5\u7269\u4ef6\u662f\u5426\u70ba\u53ef\u8b8a\u5e8f\u5217\u985e\u578b\uff08\u5982\u5217\u8868\uff09\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport collections<\/p>\n<p>if isinstance(obj, collections.MutableSequence):<br \/>\n    print(&#8220;\u9019\u662f\u4e00\u500b\u53ef\u8b8a\u5e8f\u5217&#8221;)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u4e9b\u6aa2\u67e5\u5c0d\u65bc\u7de8\u5beb\u5065\u58ef\u4e14\u6613\u65bc\u7dad\u8b77\u7684\u4ee3\u78bc\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u5728 Python \u4e2d\uff0c`isinstance()` \u662f\u4e00\u500b\u5f37\u5927\u4e14\u9748\u6d3b\u7684\u5de5\u5177\uff0c\u80fd\u5e6b\u52a9\u6211\u5011\u5feb\u901f\u6aa2\u67e5\u7269\u4ef6\u7684\u578b\u5225\u3002\u7121\u8ad6\u662f\u5728\u65e5\u5e38\u958b\u767c\u4e2d\u9084\u662f\u5728\u8655\u7406\u8907\u96dc\u7684\u985e\u7e7c\u627f\u7d50\u69cb\u6642\uff0c`isinstance()` \u90fd\u80fd\u63d0\u9ad8\u6211\u5011\u7684\u7de8\u78bc\u6548\u7387\u548c\u4ee3\u78bc\u53ef\u9760\u6027\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u985e\u578b\u6aa2\u67e5\uff0c\u60a8\u53ef\u4ee5\u53c3\u8003\u9019\u7bc7 [Python \u985e\u578b\u6aa2\u67e5\u7684\u6700\u4f73\u5be6\u8e10](https:\/\/vocus.cc\/article\/605b3a8bf8e1a80001d2e881)\u3002<\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54 (Q&#038;A)<\/h2>\n<p>**Q1: `isinstance()` \u548c `type()` \u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f**<br \/>\nA1: `isinstance()` \u4e0d\u50c5\u6aa2\u67e5\u7269\u4ef6\u7684\u985e\u578b\uff0c\u9084\u6703\u8003\u616e\u5230\u7e7c\u627f\u95dc\u4fc2\uff0c\u800c `type()` \u50c5\u6aa2\u67e5\u7269\u4ef6\u7684\u76f4\u63a5\u985e\u578b\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u4f7f\u7528 `isinstance()` \u6aa2\u67e5\u591a\u500b\u578b\u5225\uff1f**<br \/>\nA2: \u60a8\u53ef\u4ee5\u5c07\u591a\u500b\u985e\u5225\u4f5c\u70ba\u5143\u7d44\u50b3\u905e\u7d66 `isinstance()`\uff0c\u4f8b\u5982\uff1a`isinstance(obj, (int, str))`\u3002<\/p>\n<p>**Q3: \u5728\u591a\u91cd\u7e7c\u627f\u4e2d\uff0c\u5982\u4f55\u78ba\u4fdd `isinstance()` \u8fd4\u56de\u6b63\u78ba\u7684\u7d50\u679c\uff1f**<br \/>\nA3: \u5728\u591a\u91cd\u7e7c\u627f\u4e2d\uff0c`isinstance()` \u6703\u9075\u5faa Python \u7684\u65b9\u6cd5\u89e3\u6790\u9806\u5e8f\uff08MRO\uff09\uff0c\u9019\u78ba\u4fdd\u4e86\u6aa2\u67e5\u7684\u6b63\u78ba\u6027\u3002\u4f7f\u7528 `isinstance()` \u6642\uff0c\u8acb\u78ba\u4fdd\u60a8\u7684\u985e\u8a2d\u8a08\u9075\u5faa\u826f\u597d\u7684\u7e7c\u627f\u7d50\u69cb\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>instancecheck()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u6aa2\u67e5\u4e00\u500b\u5c0d\u8c61\u662f\u5426\u662f\u4e00\u500b\u7279\u5b9a\u985e\u7684\u5be6\u4f8b\u3002\u5b83\u53ef\u4ee5\u6aa2\u67e5\u5c0d\u8c61\u662f\u5426\u7e7c\u627f\u81ea\u7279\u5b9a\u7684\u985e\uff0c\u4e26\u63d0\u4f9b\u4e00\u500b\u7c21\u55ae\u7684\u65b9\u6cd5\u4f86\u6aa2\u67e5\u5c0d\u8c61\u7684\u985e\u578b\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-3324","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-RC","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3324","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=3324"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3324\/revisions"}],"predecessor-version":[{"id":3325,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3324\/revisions\/3325"}],"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=3324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}