{"id":2938,"date":"2025-06-01T16:37:26","date_gmt":"2025-06-04T09:21:25","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2938"},"modified":"2025-06-04T16:37:26","modified_gmt":"2025-06-04T09:21:25","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84isinstance%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%84isinstance%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 isinstance() \u51fd\u6578\uff1a\u985e\u578b\u6aa2\u67e5\u7684\u6700\u4f73\u5be6\u8e10\uff082025 \u66f4\u65b0\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, isinstance, \u6aa2\u67e5\u5c0d\u8c61, \u7279\u5b9a\u985e\u578b, Python \u6559\u5b78, \u7a0b\u5f0f\u8a2d\u8a08\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 isinstance() \u51fd\u6578\uff1a\u985e\u578b\u6aa2\u67e5\u7684\u6700\u4f73\u5be6\u8e10\uff082025 \u66f4\u65b0\uff09<\/h1>\n<p>Python \u7684 `isinstance()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u7528\u65bc\u6aa2\u67e5\u5c0d\u8c61\u662f\u5426\u5c6c\u65bc\u7279\u5b9a\u985e\u578b\u6216\u985e\u3002\u9019\u5728\u9032\u884c\u985e\u578b\u6aa2\u67e5\u6642\u975e\u5e38\u91cd\u8981\uff0c\u7279\u5225\u662f\u5728\u5927\u578b\u61c9\u7528\u7a0b\u5e8f\u6216\u6846\u67b6\u4e2d\u3002<\/p>\n<p>## `isinstance()` \u51fd\u6578\u7684\u8a9e\u6cd5<\/p>\n<p>`isinstance()` \u51fd\u6578\u7684\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nisinstance(object, classinfo)<br \/>\n&#8220;`<\/p>\n<p>&#8211; **object**\uff1a\u8981\u6aa2\u67e5\u7684\u5c0d\u8c61\u3002<br \/>\n&#8211; **classinfo**\uff1a\u8981\u6aa2\u67e5\u7684\u985e\u578b\u6216\u985e\uff0c\u53ef\u4ee5\u662f\u55ae\u4e00\u985e\u578b\u6216\u591a\u500b\u985e\u578b\u7684\u5143\u7d44\u3002<\/p>\n<p>`isinstance()` \u51fd\u6578\u5c07\u8fd4\u56de\u4e00\u500b\u5e03\u723e\u503c\uff0c\u82e5\u5c0d\u8c61\u5c6c\u65bc\u6307\u5b9a\u7684\u985e\u578b\uff0c\u5247\u8fd4\u56de `True`\uff0c\u5426\u5247\u8fd4\u56de `False`\u3002<\/p>\n<p>## \u57fa\u672c\u793a\u4f8b<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528 `isinstance()` \u6aa2\u67e5\u5c0d\u8c61\u7684\u985e\u578b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nx = 5<br \/>\nprint(isinstance(x, int))  # \u8f38\u51fa: True<br \/>\n&#8220;`<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u6aa2\u67e5\u4e86 `x` \u662f\u5426\u5c6c\u65bc `int` \u985e\u578b\uff0c\u7531\u65bc `x` \u662f\u4e00\u500b\u6574\u6578\uff0c\u56e0\u6b64 `isinstance()` \u51fd\u6578\u8fd4\u56de `True`\u3002<\/p>\n<p>## \u6aa2\u67e5\u81ea\u5b9a\u7fa9\u985e\u578b<\/p>\n<p>`isinstance()` \u51fd\u6578\u4e0d\u50c5\u53ef\u4ee5\u6aa2\u67e5\u5167\u5efa\u985e\u578b\uff0c\u9084\u53ef\u4ee5\u6aa2\u67e5\u81ea\u5b9a\u7fa9\u985e\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Person:<br \/>\n    pass<\/p>\n<p>p = Person()<br \/>\nprint(isinstance(p, Person))  # \u8f38\u51fa: True<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u88e1\uff0c\u6211\u5011\u6aa2\u67e5\u4e86 `p` \u662f\u5426\u5c6c\u65bc `Person` \u985e\uff0c\u7d50\u679c\u8fd4\u56de `True`\u3002<\/p>\n<p>## \u6aa2\u67e5\u591a\u500b\u985e\u578b<\/p>\n<p>`isinstance()` \u9084\u53ef\u4ee5\u6aa2\u67e5\u5c0d\u8c61\u662f\u5426\u5c6c\u65bc\u591a\u500b\u985e\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nx = 5<br \/>\nprint(isinstance(x, (int, float)))  # \u8f38\u51fa: True<br \/>\n&#8220;`<\/p>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u6aa2\u67e5 `x` \u662f\u5426\u5c6c\u65bc `int` \u6216 `float` \u985e\u578b\uff0c\u7531\u65bc `x` \u662f\u4e00\u500b\u6574\u6578\uff0c\u56e0\u6b64\u8fd4\u56de `True`\u3002<\/p>\n<p>## \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u7576\u4f7f\u7528 `isinstance()` \u6642\uff0c\u4e00\u500b\u5e38\u898b\u7684\u932f\u8aa4\u662f\u5c07 `classinfo` \u53c3\u6578\u8a2d\u7f6e\u70ba\u4e0d\u6b63\u78ba\u7684\u985e\u578b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u50b3\u905e\u4e00\u500b\u4e0d\u5b58\u5728\u7684\u985e\uff0c\u5247\u6703\u5f97\u5230 `False`\u3002\u78ba\u4fdd\u4f60\u6aa2\u67e5\u7684\u985e\u578b\u662f\u5df2\u7d93\u5b9a\u7fa9\u7684\u3002<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>`isinstance()` \u51fd\u6578\u5728\u8a31\u591a\u5834\u666f\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u591a\u578b\uff08Polymorphism\uff09\u548c\u63a5\u53e3\uff08Interface\uff09\u6642\u3002\u5b83\u53ef\u4ee5\u5e6b\u52a9\u4f60\u5beb\u51fa\u66f4\u9748\u6d3b\u7684\u4ee3\u78bc\uff0c\u4e26\u5728\u904b\u884c\u6642\u9032\u884c\u985e\u578b\u6aa2\u67e5\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5728\u5275\u5efa\u51fd\u6578\u6642\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 `isinstance()` \u78ba\u4fdd\u50b3\u905e\u7684\u53c3\u6578\u662f\u6b63\u78ba\u7684\u985e\u578b\uff1a<\/p>\n<p>&#8220;`python<br \/>\ndef process_data(data):<br \/>\n    if not isinstance(data, (list, tuple)):<br \/>\n        raise ValueError(&#8220;Data must be a list or tuple&#8221;)<br \/>\n    # \u8655\u7406\u6578\u64da\u7684\u908f\u8f2f<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u78bc\u7684\u5065\u58ef\u6027\uff0c\u4e26\u964d\u4f4e\u932f\u8aa4\u767c\u751f\u7684\u6a5f\u7387\u3002<\/p>\n<p>## \u7e3d\u7d50<\/p>\n<p>\u7e3d\u4e4b\uff0cPython \u7684 `isinstance()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u7528\u65bc\u6aa2\u67e5\u5c0d\u8c61\u7684\u985e\u578b\u3002\u5b83\u53ef\u4ee5\u7528\u65bc\u6aa2\u67e5\u5c0d\u8c61\u662f\u5426\u5c6c\u65bc\u7279\u5b9a\u985e\u578b\u6216\u985e\u578b\u7684\u96c6\u5408\uff0c\u662f\u7a0b\u5f0f\u8a2d\u8a08\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u5c0d\u65bc\u5e0c\u671b\u63d0\u5347\u4ee3\u78bc\u8cea\u91cf\u7684\u958b\u767c\u8005\u4f86\u8aaa\uff0c\u638c\u63e1 `isinstance()` \u7684\u4f7f\u7528\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<p>\u82e5\u60f3\u6df1\u5165\u4e86\u89e3 Python \u7684\u66f4\u591a\u529f\u80fd\uff0c\u8acb\u53c3\u8003 [\u9019\u88e1\u7684\u6559\u5b78](https:\/\/vocus.cc\/article\/645b5c1ef3b8ad0011b6e136)\u3002<\/p>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>### Q1: `isinstance()` \u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u54ea\u4e9b\u985e\u578b\uff1f<br \/>\nA1: `isinstance()` \u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u5167\u5efa\u985e\u578b\uff08\u5982 `int`, `float`, `str` \u7b49\uff09\u4ee5\u53ca\u81ea\u5b9a\u7fa9\u985e\u578b\u3002<\/p>\n<p>### Q2: \u5982\u4f55\u6aa2\u67e5\u4e00\u500b\u5c0d\u8c61\u662f\u5426\u5c6c\u65bc\u591a\u500b\u985e\u578b\uff1f<br \/>\nA2: \u4f60\u53ef\u4ee5\u5c07\u591a\u500b\u985e\u578b\u653e\u5165\u4e00\u500b\u5143\u7d44\u4e2d\u4f5c\u70ba `classinfo` \u53c3\u6578\u50b3\u905e\u7d66 `isinstance()`\uff0c\u4f8b\u5982 `isinstance(obj, (int, float))`\u3002<\/p>\n<p>### Q3: \u4f7f\u7528 `isinstance()` \u6709\u4ec0\u9ebc\u597d\u8655\uff1f<br \/>\nA3: \u4f7f\u7528 `isinstance()` \u53ef\u4ee5\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u548c\u5065\u58ef\u6027\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u5728\u904b\u884c\u6642\u9032\u884c\u985e\u578b\u6aa2\u67e5\uff0c\u5f9e\u800c\u6e1b\u5c11\u6f5b\u5728\u7684\u932f\u8aa4\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684isinstance()\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u7528\u65bc\u6aa2\u67e5\u5c0d\u8c61\u662f\u5426\u5c6c\u65bc\u7279\u5b9a\u985e\u578b\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u7a2e\u7c21\u55ae\u800c\u6709\u6548\u7684\u65b9\u6cd5\u4f86\u6aa2\u67e5\u5c0d\u8c61\u7684\u985e\u578b\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39isinstance()\u51fd\u6578\u7684\u7528\u6cd5\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u793a\u4f8b\u4f86\u8aaa\u660e\u5b83\u7684\u529f\u80fd\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-2938","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-Lo","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2938","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=2938"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2938\/revisions"}],"predecessor-version":[{"id":13006,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2938\/revisions\/13006"}],"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=2938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}