{"id":3106,"date":"2025-06-01T16:43:18","date_gmt":"2025-06-04T08:54:18","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3106"},"modified":"2025-06-04T16:43:18","modified_gmt":"2025-06-04T08:54:18","slug":"python%e7%9a%84instancecheck%e5%8a%9f%e8%83%bd%e4%bb%8b%e7%b4%b9","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python%e7%9a%84instancecheck%e5%8a%9f%e8%83%bd%e4%bb%8b%e7%b4%b9\/","title":{"rendered":"Python \u7684 `isinstance()` \u51fd\u5f0f\u5b8c\u6574\u4ecb\u7d39\u8207\u61c9\u7528\u7bc4\u4f8b"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, isinstance\"><\/p>\n<h1>Python \u4e2d\u7684 `isinstance()` \u51fd\u5f0f<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`isinstance()` \u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5167\u5efa\u51fd\u5f0f\uff0c\u80fd\u5920\u6aa2\u67e5\u4e00\u500b\u7269\u4ef6\u662f\u5426\u70ba\u7279\u5b9a\u985e\u5225\u7684\u5be6\u4f8b\u3002\u9019\u4e0d\u50c5\u5305\u62ec\u76f4\u63a5\u7684\u985e\u5225\u6aa2\u67e5\uff0c\u9084\u53ef\u4ee5\u6aa2\u67e5\u7269\u4ef6\u662f\u5426\u5c6c\u65bc\u67d0\u500b\u985e\u5225\u7684\u5b50\u985e\u5225\u6216\u7236\u985e\u5225\u3002\u96a8\u8457 Python \u8a9e\u6cd5\u7684\u6f14\u9032\uff0c\u4f7f\u7528 `isinstance()` \u6210\u70ba\u4e86\u6aa2\u67e5\u7269\u4ef6\u985e\u578b\u7684\u6700\u4f73\u5be6\u8e10\u4e4b\u4e00\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\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>&#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 \/>\n&#8220;`<\/p>\n<h2>`isinstance()` \u7684\u512a\u9ede<\/h2>\n<p>\u4f7f\u7528 `isinstance()` \u7684\u4e3b\u8981\u512a\u9ede\u5728\u65bc\u5b83\u80fd\u5920\u7c21\u5316\u985e\u5225\u6aa2\u67e5\u7684\u6d41\u7a0b\uff0c\u8b93\u4ee3\u78bc\u66f4\u5177\u53ef\u8b80\u6027\u3002\u6b64\u5916\uff0c\u5b83\u80fd\u5920\u6aa2\u67e5\u7269\u4ef6\u662f\u5426\u70ba\u67d0\u500b\u985e\u5225\u7684\u5b50\u985e\u5225\uff0c\u4f7f\u5f97\u4ee3\u78bc\u5728\u9762\u5c0d\u7e7c\u627f\u7d50\u69cb\u6642\u66f4\u5177\u5f48\u6027\u3002<\/p>\n<h2>`isinstance()` \u7684\u7f3a\u9ede<\/h2>\n<p>`isinstance()` \u7684\u4e3b\u8981\u9650\u5236\u662f\u5b83\u50c5\u80fd\u6aa2\u67e5\u7269\u4ef6\u662f\u5426\u70ba\u7279\u5b9a\u985e\u5225\u7684\u5be6\u4f8b\uff0c\u5c0d\u65bc\u67d0\u4e9b\u8907\u96dc\u7684\u578b\u5225\u6aa2\u67e5\u9700\u6c42\uff0c\u53ef\u80fd\u9700\u8981\u984d\u5916\u7684\u908f\u8f2f\u4f86\u5224\u65b7\u3002<\/p>\n<h2>`isinstance()` \u7684\u61c9\u7528<\/h2>\n<p>`isinstance()` \u53ef\u4ee5\u7528\u65bc\u591a\u7a2e\u5834\u666f\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u78ba\u4fdd\u7269\u4ef6\u985e\u578b\u7684\u60c5\u6cc1\u4e0b\u3002\u4f8b\u5982\uff0c\u6aa2\u67e5\u7269\u4ef6\u662f\u5426\u70ba\u5b57\u4e32\u985e\u578b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nif isinstance(obj, str):<br \/>\n    # \u9032\u884c\u5b57\u4e32\u76f8\u95dc\u8655\u7406<br \/>\n&#8220;`<\/p>\n<p>\u6b64\u5916\uff0c`isinstance()` \u4e5f\u53ef\u4ee5\u6aa2\u67e5\u7269\u4ef6\u662f\u5426\u70ba\u6578\u503c\u985e\u578b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nif isinstance(obj, (int, float)):<br \/>\n    # \u9032\u884c\u6578\u503c\u76f8\u95dc\u8655\u7406<br \/>\n&#8220;`<\/p>\n<p>\u751a\u81f3\u53ef\u4ee5\u6aa2\u67e5\u7269\u4ef6\u662f\u5426\u70ba\u53ef\u8b8a\u985e\u578b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport collections<\/p>\n<p>if isinstance(obj, collections.MutableSequence):<br \/>\n    # \u9032\u884c\u53ef\u8b8a\u5e8f\u5217\u76f8\u95dc\u8655\u7406<br \/>\n&#8220;`<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Python \u4e2d\u7684 `isinstance()` \u51fd\u5f0f\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u80fd\u5920\u6709\u6548\u6aa2\u67e5\u7269\u4ef6\u7684\u985e\u578b\u548c\u7e7c\u627f\u95dc\u4fc2\u3002\u9019\u4f7f\u5f97\u958b\u767c\u8005\u5728\u7de8\u5beb\u4ee3\u78bc\u6642\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u78ba\u8a8d\u7269\u4ef6\u7684\u6b63\u78ba\u6027\uff0c\u4e26\u907f\u514d\u6f5b\u5728\u7684\u932f\u8aa4\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u66f4\u591a\u5be6\u7528\u6280\u5de7\uff0c\u8acb\u53c3\u95b1\u6211\u5011\u7684<a href=\"https:\/\/vocus.cc\" target=\"_blank\" rel=\"noopener\">Python \u6559\u5b78\u8cc7\u6e90<\/a>\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. `isinstance()` \u548c `type()` \u6709\u4ec0\u9ebc\u5340\u5225\uff1f<\/h3>\n<p>`isinstance()` \u4e0d\u50c5\u6aa2\u67e5\u7269\u4ef6\u7684\u76f4\u63a5\u985e\u578b\uff0c\u9084\u80fd\u6aa2\u67e5\u5176\u662f\u5426\u70ba\u5b50\u985e\u5225\u7684\u5be6\u4f8b\uff0c\u800c `type()` \u50c5\u8fd4\u56de\u7269\u4ef6\u7684\u76f4\u63a5\u985e\u578b\u3002<\/p>\n<h3>2. \u6211\u53ef\u4ee5\u5c07\u591a\u500b\u985e\u5225\u50b3\u905e\u7d66 `isinstance()` \u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u5c07\u4e00\u500b\u985e\u5225\u5143\u7d44\u50b3\u905e\u7d66 `isinstance()`\uff0c\u9019\u6a23\u53ef\u4ee5\u6aa2\u67e5\u7269\u4ef6\u662f\u5426\u5c6c\u65bc\u591a\u500b\u985e\u5225\u4e2d\u7684\u4efb\u4f55\u4e00\u500b\u3002<\/p>\n<h3>3. \u4f7f\u7528 `isinstance()` \u6aa2\u67e5\u81ea\u5b9a\u7fa9\u985e\u5225\u7684\u5be6\u4f8b\u6709\u4ec0\u9ebc\u7279\u5225\u4e4b\u8655\uff1f<\/h3>\n<p>\u7576\u4f7f\u7528\u81ea\u5b9a\u7fa9\u985e\u5225\u6642\uff0c`isinstance()` \u4ecd\u7136\u6709\u6548\uff0c\u56e0\u70ba\u5b83\u9075\u5faa Python \u7684\u7e7c\u627f\u898f\u5247\uff0c\u80fd\u5920\u6b63\u78ba\u5224\u65b7\u7269\u4ef6\u7684\u985e\u578b\u5c64\u7d1a\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u7684instancecheck()\u51fd\u6578\u53ef\u4ee5\u6aa2\u67e5\u4e00\u500b\u5c0d\u8c61\u662f\u5426\u662f\u4e00\u500b\u7279\u5b9a\u985e\u5225\u7684\u5be6\u4f8b\uff0c\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u5c0d\u8c61\u7684\u985e\u578b\uff0c\u4e26\u78ba\u4fdd\u7a0b\u5e8f\u7684\u6b63\u78ba\u6027\u3002\u672c\u6587\u5c07\u4ecb\u7d39instancecheck()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\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-3106","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-O6","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3106","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=3106"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3106\/revisions"}],"predecessor-version":[{"id":3107,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3106\/revisions\/3107"}],"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=3106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}