{"id":922064,"date":"2024-12-26T14:24:57","date_gmt":"2024-12-26T06:24:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/922064.html"},"modified":"2024-12-26T14:24:59","modified_gmt":"2024-12-26T06:24:59","slug":"python-%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/922064.html","title":{"rendered":"python \u5982\u4f55\u5224\u65ad\u7c7b\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210736\/a2e50df0-730c-470f-8693-743657bf71f7.webp\" alt=\"python \u5982\u4f55\u5224\u65ad\u7c7b\u578b\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5224\u65ad\u7c7b\u578b\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>type()<\/code>\u51fd\u6570\u3001<code>isinstance()<\/code>\u51fd\u6570\u3001<code>types<\/code>\u6a21\u5757\u7b49\u3002<\/strong><code>isinstance()<\/code>\u51fd\u6570\u66f4\u5e38\u7528\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u80fd\u5224\u65ad\u5bf9\u8c61\u662f\u5426\u662f\u67d0\u4e2a\u7c7b\u7684\u5b9e\u4f8b\uff0c\u8fd8\u53ef\u4ee5\u5224\u65ad\u5bf9\u8c61\u662f\u5426\u662f\u5176\u7236\u7c7b\u7684\u5b9e\u4f8b\uff0c\u8fd9\u5bf9\u4e8e\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u7684\u591a\u6001\u975e\u5e38\u6709\u7528\u3002<strong>\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u89e3\u91ca\u5b83\u4eec\u7684\u9002\u7528\u573a\u666f\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001TYPE()\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>type()<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u7528\u4e8e\u5224\u65ad\u5bf9\u8c61\u7c7b\u578b\u7684\u51fd\u6570\u3002\u5b83\u8fd4\u56de\u7684\u662f\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u4e0d\u8003\u8651\u7ee7\u627f\u5173\u7cfb\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>type()<\/code>\u53ef\u4ee5\u7b80\u5355\u5730\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>print(type(a))  # \u8f93\u51fa\uff1a&lt;class &#39;int&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>type(a)<\/code>\u8fd4\u56de\u4e86\u53d8\u91cf<code>a<\/code>\u7684\u7c7b\u578b\uff0c\u5373\u6574\u578b\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7c7b\u578b\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<p><p><code>type()<\/code>\u8fd4\u56de\u7684\u7ed3\u679c\u53ef\u4ee5\u4e0ePython\u7684\u5185\u7f6e\u7c7b\u578b\u76f4\u63a5\u6bd4\u8f83\u6765\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u5c5e\u4e8e\u67d0\u79cd\u7c7b\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if type(a) == int:<\/p>\n<p>    print(&quot;a \u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u867d\u7136<code>type()<\/code>\u53ef\u4ee5\u7528\u4e8e\u7c7b\u578b\u5224\u65ad\uff0c\u4f46\u5728\u6d89\u53ca\u7ee7\u627f\u7684\u60c5\u51b5\u4e0b\uff0c\u5b83\u7684\u4f5c\u7528\u6709\u9650<\/strong>\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4f1a\u8003\u8651\u5230\u5b50\u7c7b\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5c40\u9650\u6027<\/strong><\/p>\n<\/p>\n<p><p><code>type()<\/code>\u4e0d\u652f\u6301\u7ee7\u627f\u94fe\u7684\u5224\u65ad\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u5b50\u7c7b\u5b9e\u4f8b\uff0c<code>type()<\/code>\u5224\u65ad\u5b83\u7684\u7c7b\u578b\u65f6\uff0c\u4e0d\u4f1a\u8ba4\u4e3a\u5b83\u662f\u7236\u7c7b\u7684\u7c7b\u578b\u3002\u8fd9\u662f<code>type()<\/code>\u7684\u4e00\u4e2a\u5c40\u9650\u6027\uff0c\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u5c24\u5176\u9700\u8981\u6ce8\u610f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001ISINSTANCE()\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>isinstance()<\/code>\u51fd\u6570\u662f\u5224\u65ad\u5bf9\u8c61\u7c7b\u578b\u7684\u53e6\u4e00\u79cd\u65b9\u6cd5\uff0c\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u5224\u65ad\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u8fd8\u53ef\u4ee5\u5224\u65ad\u5bf9\u8c61\u662f\u5426\u662f\u5176\u7236\u7c7b\u7684\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p><code>isinstance()<\/code>\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>isinstance(object, classinfo)<\/code>\uff0c\u8fd4\u56de<code>True<\/code>\u6216<code>False<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Animal:<\/p>\n<p>    pass<\/p>\n<p>class Dog(Animal):<\/p>\n<p>    pass<\/p>\n<p>d = Dog()<\/p>\n<p>print(isinstance(d, Dog))  # \u8f93\u51fa\uff1aTrue<\/p>\n<p>print(isinstance(d, Animal))  # \u8f93\u51fa\uff1aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>isinstance()<\/code>\u4e0d\u4ec5\u5224\u65ad\u4e86<code>d<\/code>\u662f<code>Dog<\/code>\u7c7b\u7684\u5b9e\u4f8b\uff0c\u8fd8\u5224\u65ad\u4e86<code>d<\/code>\u662f<code>Animal<\/code>\u7c7b\u7684\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u591a\u7c7b\u578b\u5224\u65ad<\/strong><\/p>\n<\/p>\n<p><p><code>isinstance()<\/code>\u53ef\u4ee5\u5224\u65ad\u591a\u4e2a\u7c7b\u578b\uff0c\u53ea\u9700\u8981\u5c06\u7c7b\u578b\u653e\u5165\u4e00\u4e2a\u5143\u7ec4\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if isinstance(d, (Dog, Animal)):<\/p>\n<p>    print(&quot;d \u662f Dog \u6216 Animal \u7684\u5b9e\u4f8b&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u529f\u80fd\u4f7f\u5f97<code>isinstance()<\/code>\u5728\u9700\u8981\u5224\u65ad\u591a\u79cd\u53ef\u80fd\u7c7b\u578b\u65f6\u975e\u5e38\u4fbf\u5229\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u70b9<\/strong><\/p>\n<\/p>\n<p><p><strong><code>isinstance()<\/code>\u7684\u6700\u5927\u4f18\u70b9\u662f\u652f\u6301\u7ee7\u627f\u5173\u7cfb\u5224\u65ad<\/strong>\uff0c\u8fd9\u4f7f\u5f97\u5b83\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u66f4\u52a0\u7075\u6d3b\u548c\u5f3a\u5927\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001TYPES\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u7684<code>types<\/code>\u6a21\u5757\u5b9a\u4e49\u4e86\u8bb8\u591a\u4e0ePython\u5185\u7f6e\u7c7b\u578b\u5bf9\u5e94\u7684\u5e38\u91cf\uff0c\u901a\u5e38\u7528\u4e8e\u5224\u65ad\u7279\u5b9a\u7684\u5185\u7f6e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p><code>types<\/code>\u6a21\u5757\u4e2d\u7684\u5e38\u91cf\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u7279\u5b9a\u7684\u5185\u7f6e\u7c7b\u578b\uff0c\u4f8b\u5982\u51fd\u6570\u3001\u65b9\u6cd5\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import types<\/p>\n<p>def func():<\/p>\n<p>    pass<\/p>\n<p>print(isinstance(func, types.FunctionType))  # \u8f93\u51fa\uff1aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>types.FunctionType<\/code>\u6765\u5224\u65ad<code>func<\/code>\u662f\u5426\u4e3a\u4e00\u4e2a\u51fd\u6570\u7c7b\u578b\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5176\u4ed6\u5e38\u91cf<\/strong><\/p>\n<\/p>\n<p><p><code>types<\/code>\u6a21\u5757\u4e2d\u8fd8\u5305\u542b\u5176\u4ed6\u7528\u4e8e\u5224\u65ad\u751f\u6210\u5668\u3001\u65b9\u6cd5\u3001\u6a21\u5757\u7b49\u7c7b\u578b\u7684\u5e38\u91cf\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>types.ModuleType<\/code><\/li>\n<li><code>types.MethodType<\/code><\/li>\n<li><code>types.GeneratorType<\/code><\/li>\n<\/ul>\n<p><p>\u8fd9\u4e9b\u5e38\u91cf\u53ef\u4ee5\u7528\u4e8e\u66f4\u52a0\u7ec6\u81f4\u7684\u7c7b\u578b\u5224\u65ad\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528\u573a\u666f\u4e0e\u5efa\u8bae<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u4f7f\u7528<code>type()<\/code>\u8fd8\u662f<code>isinstance()<\/code>\u65f6\uff0c\u5e94\u8be5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u3002\u5982\u679c\u4e0d\u6d89\u53ca\u7ee7\u627f\u5173\u7cfb\uff0c\u53ef\u4ee5\u4f7f\u7528<code>type()<\/code>\uff0c\u800c\u5982\u679c\u9700\u8981\u8003\u8651\u7ee7\u627f\u5173\u7cfb\uff0c\u5c24\u5176\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u5efa\u8bae\u4f7f\u7528<code>isinstance()<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4ee3\u7801\u53ef\u8bfb\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff0c\u4f7f\u7528<code>isinstance()<\/code>\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u5c24\u5176\u662f\u5728\u591a\u6001\u5bf9\u8c61\u7684\u5224\u65ad\u4e2d\uff0c\u5b83\u80fd\u66f4\u6e05\u6670\u5730\u8868\u8fbe\u5224\u65ad\u610f\u56fe\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u8003\u8651<\/strong><\/p>\n<\/p>\n<p><p>\u867d\u7136<code>type()<\/code>\u548c<code>isinstance()<\/code>\u5728\u6027\u80fd\u4e0a\u7684\u5dee\u5f02\u901a\u5e38\u53ef\u4ee5\u5ffd\u7565\u4e0d\u8ba1\uff0c\u4f46\u5728\u9ad8\u6027\u80fd\u9700\u6c42\u7684\u573a\u666f\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u6d4b\u8bd5\u548c\u9009\u62e9\u66f4\u4f18\u7684\u65b9\u6848\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u603b\u7684\u6765\u8bf4\uff0c<strong>\u5728Python\u4e2d\u5224\u65ad\u7c7b\u578b\u7684\u65b9\u6cd5\u7075\u6d3b\u591a\u6837<\/strong>\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5c24\u5176\u662f\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u9009\u62e9\u80fd\u591f\u6b63\u786e\u5904\u7406\u7ee7\u627f\u5173\u7cfb\u7684\u7c7b\u578b\u5224\u65ad\u65b9\u6cd5\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u7684\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>type()<\/code>\u6765\u5224\u65ad\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff0c<code>type(variable)<\/code>\u5c06\u8fd4\u56de\u8be5\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u5982\u679c\u9700\u8981\u8fdb\u884c\u66f4\u5177\u4f53\u7684\u7c7b\u578b\u5224\u65ad\uff0c\u53ef\u4ee5\u4f7f\u7528<code>isinstance()<\/code>\u51fd\u6570\uff0c\u5b83\u5141\u8bb8\u4f60\u68c0\u67e5\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u67d0\u4e2a\u7c7b\u6216\u5176\u5b50\u7c7b\u7684\u5b9e\u4f8b\u3002\u6bd4\u5982\uff0c<code>isinstance(variable, int)<\/code>\u4f1a\u8fd4\u56de<code>True<\/code>\uff0c\u5982\u679c<code>variable<\/code>\u662f\u6574\u6570\u7c7b\u578b\u3002<\/p>\n<p><strong>\u5224\u65ad\u7c7b\u578b\u65f6\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u9700\u8981\u907f\u514d\uff1f<\/strong><br \/>\u5728\u5224\u65ad\u7c7b\u578b\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u6df7\u6dc6<code>type()<\/code>\u548c<code>isinstance()<\/code>\u7684\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u76f4\u63a5\u4f7f\u7528<code>type(variable) == int<\/code>\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5728\u5b50\u7c7b\u4e2d\u5224\u65ad\u5931\u8d25\uff0c\u800c\u4f7f\u7528<code>isinstance()<\/code>\u5219\u80fd\u6b63\u786e\u8bc6\u522b\u3002\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u786e\u4fdd\u68c0\u67e5\u6240\u6709\u5d4c\u5957\u5c42\u7ea7\u7684\u7c7b\u578b\u4e5f\u975e\u5e38\u91cd\u8981\uff0c\u4ee5\u907f\u514d\u6f5c\u5728\u7684\u903b\u8f91\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u4f55\u5224\u65ad\u81ea\u5b9a\u4e49\u7c7b\u7684\u5b9e\u4f8b\u7c7b\u578b\uff1f<\/strong><br \/>\u5bf9\u4e8e\u81ea\u5b9a\u4e49\u7c7b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>type()<\/code>\u548c<code>isinstance()<\/code>\u6765\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u8be5\u7c7b\u7684\u5b9e\u4f8b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a<code>MyClass<\/code>\u7684\u81ea\u5b9a\u4e49\u7c7b\uff0c\u53ef\u4ee5\u901a\u8fc7<code>isinstance(obj, MyClass)<\/code>\u6765\u5224\u65ad<code>obj<\/code>\u662f\u5426\u662f<code>MyClass<\/code>\u7684\u5b9e\u4f8b\u3002\u4f7f\u7528<code>isinstance()<\/code>\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u53ef\u4ee5\u8003\u8651\u7ee7\u627f\u5173\u7cfb\uff0c\u4ece\u800c\u786e\u4fdd\u66f4\u7075\u6d3b\u7684\u7c7b\u578b\u68c0\u67e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5224\u65ad\u7c7b\u578b\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528type()\u51fd\u6570\u3001isinstance()\u51fd\u6570\u3001types\u6a21\u5757\u7b49\u3002isi [&hellip;]","protected":false},"author":3,"featured_media":922068,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/922064"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=922064"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/922064\/revisions"}],"predecessor-version":[{"id":922069,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/922064\/revisions\/922069"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/922068"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=922064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=922064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=922064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}