{"id":3468,"date":"2025-05-31T15:14:43","date_gmt":"2025-06-04T07:39:42","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3468"},"modified":"2025-06-04T15:14:43","modified_gmt":"2025-06-04T07:39:42","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84instancecheck%e5%87%bd%e6%95%b8%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84instancecheck%e5%87%bd%e6%95%b8%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u6df1\u5165\u89e3\u6790 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, instancecheck, \u985e\u5225\u6aa2\u67e5\"><\/p>\n<h1>\u6df1\u5165\u89e3\u6790 Python \u4e2d\u7684 <code>isinstance()<\/code> \u51fd\u6578<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`isinstance()` \u662f\u4e00\u500b\u6975\u5176\u6709\u7528\u7684\u5167\u5efa\u51fd\u6578\uff0c\u7528\u65bc\u6aa2\u67e5\u4e00\u500b\u7269\u4ef6\u662f\u5426\u70ba\u67d0\u500b\u985e\u5225\u7684\u5be6\u4f8b\u6216\u5176\u5b50\u985e\u5225\u7684\u5be6\u4f8b\u3002\u9019\u500b\u51fd\u6578\u4e0d\u50c5\u80fd\u5e6b\u52a9\u6211\u5011\u9032\u884c\u985e\u5225\u9a57\u8b49\uff0c\u9084\u80fd\u63d0\u9ad8\u7a0b\u5f0f\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002\u672c\u6587\u5c07\u70ba\u60a8\u4ecb\u7d39 `isinstance()` \u7684\u4f7f\u7528\u65b9\u6cd5\u3001\u512a\u7f3a\u9ede\u4ee5\u53ca\u5be6\u969b\u61c9\u7528\u7bc4\u4f8b\uff0c\u4e26\u4e14\u66f4\u65b0\u70ba 2025 \u5e74\u6700\u65b0\u7684\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u4f7f\u7528\u65b9\u6cd5<\/h2>\n<p>`isinstance()` \u7684\u4f7f\u7528\u65b9\u6cd5\u975e\u5e38\u7c21\u55ae\uff0c\u60a8\u53ea\u9700\u50b3\u5165\u5169\u500b\u53c3\u6578\uff1a\u7b2c\u4e00\u500b\u662f\u8981\u6aa2\u67e5\u7684\u7269\u4ef6\uff0c\u7b2c\u4e8c\u500b\u662f\u8981\u6aa2\u67e5\u7684\u985e\u5225\u6216\u985e\u5225\u5143\u7d44\u3002\u5982\u679c\u50b3\u5165\u7684\u7269\u4ef6\u662f\u8a72\u985e\u5225\u6216\u5176\u5b50\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 `isinstance()` \u7684\u57fa\u672c\u7528\u6cd5\u7bc4\u4f8b\uff1a<\/p>\n<pre class=\"brush: python\">\nclass A:\n    pass\n\nclass B(A):\n    pass\n\na = A()\nb = B()\n\nprint(isinstance(a, A)) # True\nprint(isinstance(b, A)) # True\nprint(isinstance(b, B)) # True\nprint(isinstance(a, B)) # False\n<\/pre>\n<h2>\u512a\u9ede<\/h2>\n<p>\u4f7f\u7528 `isinstance()` \u7684\u512a\u9ede\u5305\u62ec\uff1a<br \/>\n&#8211; **\u7c21\u55ae\u76f4\u89c0**\uff1a\u8b93\u7a0b\u5f0f\u78bc\u66f4\u5177\u53ef\u8b80\u6027\uff0c\u958b\u767c\u8005\u6613\u65bc\u7406\u89e3\u3002<br \/>\n&#8211; **\u9748\u6d3b\u6027**\uff1a\u53ef\u4ee5\u6aa2\u67e5\u985e\u5225\u53ca\u5176\u5b50\u985e\u5225\uff0c\u4f7f\u5f97\u985e\u5225\u95dc\u4fc2\u7684\u9a57\u8b49\u66f4\u70ba\u9748\u6d3b\u3002<\/p>\n<h2>\u7f3a\u9ede<\/h2>\n<p>\u4e0d\u904e\uff0c`isinstance()` \u4e5f\u6709\u5176\u9650\u5236\uff1a<br \/>\n&#8211; **\u53ef\u80fd\u904e\u65bc\u5bec\u9b06**\uff1a\u5c0d\u65bc\u67d0\u4e9b\u60c5\u6cc1\uff0c\u60a8\u53ef\u80fd\u53ea\u60f3\u6aa2\u67e5\u7279\u5b9a\u985e\u5225\uff0c\u800c\u4e0d\u5e0c\u671b\u5305\u542b\u5b50\u985e\u5225\u3002<\/p>\n<h2>\u5be6\u969b\u61c9\u7528<\/h2>\n<p>`isinstance()` \u975e\u5e38\u9069\u5408\u7528\u65bc\u8cc7\u6599\u9a45\u52d5\u7684\u61c9\u7528\u7a0b\u5f0f\u4e2d\uff0c\u5c24\u5176\u662f\u5728\u8655\u7406\u8907\u96dc\u7684\u7269\u4ef6\u7d50\u69cb\u6642\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u5be6\u969b\u7684\u61c9\u7528\u7bc4\u4f8b\uff1a<\/p>\n<pre class=\"brush: python\">\ndef check_instance(obj):\n    if isinstance(obj, A):\n        print(\"This object is an instance of A or its subclass.\")\n    else:\n        print(\"This object is not an instance of A.\")\n\ncheck_instance(b)  # \u8f38\u51fa: This object is an instance of A or its subclass.\ncheck_instance(a)  # \u8f38\u51fa: This object is an instance of A or its subclass.\n<\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c`check_instance` \u51fd\u6578\u53ef\u4ee5\u9748\u6d3b\u5730\u6aa2\u67e5\u7269\u4ef6\u7684\u985e\u5225\uff0c\u4e26\u6839\u64da\u7d50\u679c\u8f38\u51fa\u76f8\u61c9\u7684\u8a0a\u606f\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `isinstance()` \u6642\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u554f\u984c\uff1a<br \/>\n1. **\u6aa2\u67e5\u7d50\u679c\u4e0d\u6b63\u78ba**\uff1a\u78ba\u4fdd\u60a8\u50b3\u905e\u7684\u53c3\u6578\u6b63\u78ba\uff0c\u4e14\u985e\u5225\u540d\u7a31\u7121\u8aa4\u3002<br \/>\n2. **\u4f7f\u7528\u4e0d\u7576**\uff1a\u5c0d\u65bc\u8f03\u8907\u96dc\u7684\u985e\u5225\u7d50\u69cb\uff0c\u53ef\u80fd\u9700\u8981\u9032\u4e00\u6b65\u6aa2\u67e5\u4ee5\u78ba\u4fdd\u908f\u8f2f\u6b63\u78ba\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`isinstance()` \u4e0d\u50c5\u53ef\u4ee5\u7528\u65bc\u57fa\u672c\u7684\u985e\u5225\u6aa2\u67e5\uff0c\u9084\u53ef\u4ee5\u8207\u5176\u4ed6\u529f\u80fd\u7d50\u5408\u4f7f\u7528\uff0c\u4f8b\u5982\uff1a<br \/>\n&#8211; **\u7570\u5e38\u8655\u7406**\uff1a\u5728\u6355\u6349\u7570\u5e38\u6642\uff0c\u6aa2\u67e5\u932f\u8aa4\u985e\u578b\u3002<br \/>\n&#8211; **API \u9a57\u8b49**\uff1a\u5728\u63a5\u6536\u7269\u4ef6\u6642\uff0c\u78ba\u4fdd\u5b83\u7b26\u5408\u9810\u671f\u7684\u985e\u5225\u3002<\/p>\n<p>\u6700\u5f8c\uff0c\u82e5\u60a8\u60f3\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u985e\u5225\u8207\u7269\u4ef6\u5c0e\u5411\u7de8\u7a0b\uff0c\u6b61\u8fce\u53c3\u8003 [\u9019\u7bc7\u6587\u7ae0](https:\/\/vocus.cc\/article\/605cf2a7f628740056f5a1b2) \u4f86\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. `isinstance()` \u8207 `type()` \u6709\u4f55\u4e0d\u540c\uff1f<\/h3>\n<p>`isinstance()` \u6703\u6aa2\u67e5\u7269\u4ef6\u662f\u5426\u70ba\u67d0\u500b\u985e\u5225\u6216\u5176\u5b50\u985e\u5225\u7684\u5be6\u4f8b\uff0c\u800c `type()` \u50c5\u6703\u6aa2\u67e5\u7269\u4ef6\u7684\u7cbe\u78ba\u985e\u578b\u3002<\/p>\n<h3>2. \u5982\u4f55\u6aa2\u67e5\u591a\u500b\u985e\u5225\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u5c07\u985e\u5225\u653e\u5165\u5143\u7d44\u4e2d\uff0c\u4f8b\u5982\uff1a`isinstance(obj, (A, B))`\u3002<\/p>\n<h3>3. \u5728\u4ec0\u9ebc\u60c5\u6cc1\u4e0b\u61c9\u4f7f\u7528 `isinstance()`\uff1f<\/h3>\n<p>\u7576\u60a8\u9700\u8981\u6aa2\u67e5\u7269\u4ef6\u662f\u5426\u5c6c\u65bc\u67d0\u500b\u985e\u5225\u6216\u5176\u5b50\u985e\u5225\u6642\uff0c\u4f7f\u7528 `isinstance()` \u662f\u6700\u4f73\u9078\u64c7\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>instancecheck()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u5167\u5efa\u51fd\u6578\uff0c\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u4e00\u500b\u5c0d\u8c61\u662f\u5426\u662f\u4e00\u500b\u7279\u5b9a\u985e\u5225\u7684\u5be6\u4f8b\uff0c\u6216\u8005\u662f\u4e00\u500b\u7279\u5b9a\u985e\u5225\u7684\u5b50\u985e\u5225\u7684\u5be6\u4f8b\u3002\u5b83\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u63a7\u5236\u5c0d\u8c61\u7684\u884c\u70ba\uff0c\u4e26\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\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-3468","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-TW","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3468","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=3468"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3468\/revisions"}],"predecessor-version":[{"id":3469,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3468\/revisions\/3469"}],"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=3468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}