{"id":997537,"date":"2024-12-27T09:25:03","date_gmt":"2024-12-27T01:25:03","guid":{"rendered":""},"modified":"2024-12-27T09:25:07","modified_gmt":"2024-12-27T01:25:07","slug":"%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%adpython%e6%95%b0%e5%80%bc%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/997537.html","title":{"rendered":"\u5982\u4f55\u5224\u65adpython\u6570\u503c\u7c7b\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073324\/aae22ae9-ba36-4922-8c68-7caeffc7dbda.webp\" alt=\"\u5982\u4f55\u5224\u65adpython\u6570\u503c\u7c7b\u578b\" \/><\/p>\n<p><h3>\u4e00\u3001\u5224\u65ad Python \u6570\u503c\u7c7b\u578b\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u6570\u503c\u7c7b\u578b\u4e3b\u8981\u5305\u62ec\u6574\u6570\uff08int\uff09\u3001\u6d6e\u70b9\u6570\uff08float\uff09\u3001\u590d\u6570\uff08complex\uff09\u3002\u8981\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u5c5e\u4e8e\u54ea\u79cd\u6570\u503c\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<strong><code>type()<\/code>\u3001<code>isinstance()<\/code>\u3001\u4ee5\u53ca<code>is<\/code>\u8fd0\u7b97\u7b26<\/strong>\u3002\u5176\u4e2d\uff0c<code>isinstance()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u7684\u7c7b\u578b\uff0c\u8fd8\u53ef\u4ee5\u68c0\u67e5\u662f\u5426\u5c5e\u4e8e\u67d0\u4e2a\u7c7b\u578b\u7684\u5b50\u7c7b\u3002<strong>\u4f7f\u7528<code>type()<\/code>\u51fd\u6570\u53ef\u4ee5\u83b7\u53d6\u53d8\u91cf\u7684\u7c7b\u578b\uff0c\u4f46\u4e0d\u9002\u7528\u4e8e\u5b50\u7c7b\u5224\u65ad<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528 <code>type()<\/code> \u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>type()<\/code> \u662f Python \u7684\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u8fd4\u56de\u5bf9\u8c61\u7684\u7c7b\u578b\u3002\u8981\u5224\u65ad\u67d0\u4e2a\u53d8\u91cf\u662f\u5426\u662f\u67d0\u79cd\u6570\u503c\u7c7b\u578b\uff0c\u53ef\u4ee5\u5c06 <code>type()<\/code> \u7684\u8fd4\u56de\u503c\u4e0e\u76f8\u5e94\u7684\u7c7b\u578b\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>if type(x) is int:<\/p>\n<p>    print(&quot;x \u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u7b80\u5355\uff0c\u4f46\u4e0d\u9002\u7528\u4e8e\u7ee7\u627f\u5173\u7cfb\u5224\u65ad\u3002\u4f8b\u5982\uff0c\u81ea\u5b9a\u4e49\u7c7b\u7ee7\u627f\u81ea <code>int<\/code> \u65f6\uff0c<code>type()<\/code> \u5c06\u4e0d\u8ba4\u4e3a\u5176\u4e3a <code>int<\/code> \u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528 <code>isinstance()<\/code> \u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>isinstance()<\/code> \u51fd\u6570\u7528\u4e8e\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u4e3a\u6307\u5b9a\u7684\u7c7b\u578b\u6216\u5176\u5b50\u7c7b\u7684\u5b9e\u4f8b\u3002\u76f8\u6bd4 <code>type()<\/code>\uff0c<code>isinstance()<\/code> \u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10.5<\/p>\n<p>if isinstance(x, float):<\/p>\n<p>    print(&quot;x \u662f\u4e00\u4e2a\u6d6e\u70b9\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>isinstance()<\/code> \u662f\u63a8\u8350\u4f7f\u7528\u7684\u65b9\u6cd5\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5224\u65ad\u590d\u6742\u7c7b\u578b\u6216\u8003\u8651\u7ee7\u627f\u5173\u7cfb\u65f6\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6709\u6548\u5224\u65ad\u53d8\u91cf\u7684\u6570\u503c\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>3. \u4f7f\u7528 <code>is<\/code> \u8fd0\u7b97\u7b26<\/h4>\n<\/p>\n<p><p><code>is<\/code> \u8fd0\u7b97\u7b26\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u8eab\u4efd\uff0c\u800c\u4e0d\u662f\u503c\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4e0d\u5efa\u8bae\u4f7f\u7528 <code>is<\/code> \u6765\u5224\u65ad\u53d8\u91cf\u7c7b\u578b\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 1 + 2j<\/p>\n<p>if type(x) is complex:<\/p>\n<p>    print(&quot;x \u662f\u4e00\u4e2a\u590d\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136 <code>is<\/code> \u53ef\u4ee5\u7528\u4e8e\u7c7b\u578b\u5224\u65ad\uff0c\u4f46\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c<code>isinstance()<\/code> \u662f\u66f4\u4e3a\u4f18\u96c5\u548c\u5b89\u5168\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6574\u6570\uff08int\uff09\u7c7b\u578b\u5224\u65ad<\/h3>\n<\/p>\n<p><h4>1. <code>type()<\/code> \u548c <code>isinstance()<\/code> \u7684\u533a\u522b<\/h4>\n<\/p>\n<p><p>\u867d\u7136 <code>type()<\/code> \u53ef\u4ee5\u76f4\u63a5\u5224\u65ad\u6574\u6570\u7c7b\u578b\uff0c\u4f46\u5728\u7ee7\u627f\u548c\u6269\u5c55\u4e2d\uff0c<code>isinstance()<\/code> \u66f4\u4e3a\u6709\u6548\u3002\u4f8b\u5982\uff0c\u5f53\u81ea\u5b9a\u4e49\u7c7b\u7ee7\u627f\u81ea <code>int<\/code> \u65f6\uff0c<code>isinstance()<\/code> \u53ef\u4ee5\u8bc6\u522b\u51fa\u8fd9\u79cd\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyInt(int):<\/p>\n<p>    pass<\/p>\n<p>y = MyInt(5)<\/p>\n<p>print(type(y) is int)  # \u8f93\u51fa False<\/p>\n<p>print(isinstance(y, int))  # \u8f93\u51fa True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8fb9\u754c\u60c5\u51b5<\/h4>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u6574\u6570\u7c7b\u578b\u6ca1\u6709\u4e0a\u9650\u3002\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u5904\u7406\u975e\u5e38\u5927\u7684\u6574\u6570\uff0c\u800c\u4e0d\u5fc5\u62c5\u5fc3\u6ea2\u51fa\u3002Python \u81ea\u52a8\u5c06\u8fd9\u4e9b\u6574\u6570\u5904\u7406\u4e3a\u957f\u6574\u578b\uff0c\u56e0\u6b64\u5728\u5904\u7406\u5927\u6574\u6570\u65f6\uff0c<code>isinstance()<\/code> \u5224\u65ad\u4f9d\u65e7\u6709\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">big_num = 10100<\/p>\n<p>print(isinstance(big_num, int))  # \u8f93\u51fa True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6d6e\u70b9\u6570\uff08float\uff09\u7c7b\u578b\u5224\u65ad<\/h3>\n<\/p>\n<p><h4>1. \u7cbe\u5ea6\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u6d6e\u70b9\u6570\u5728\u8ba1\u7b97\u673a\u4e2d\u901a\u5e38\u4f7f\u7528\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\uff0864\u4f4d\uff09\u8868\u793a\u3002\u7531\u4e8e\u6d6e\u70b9\u6570\u7684\u8868\u793a\u65b9\u5f0f\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7cbe\u5ea6\u95ee\u9898\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528 <code>isinstance()<\/code> \u5224\u65ad\u7c7b\u578b\u4f9d\u65e7\u6709\u6548\uff0c\u4f46\u8981\u6ce8\u610f\u8ba1\u7b97\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 0.1 + 0.2<\/p>\n<p>print(a)  # \u8f93\u51fa\u53ef\u80fd\u4e0d\u5b8c\u5168\u662f 0.3<\/p>\n<p>print(isinstance(a, float))  # \u8f93\u51fa True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7279\u6b8a\u503c<\/h4>\n<\/p>\n<p><p>\u6d6e\u70b9\u6570\u7c7b\u578b\u4e2d\u8fd8\u5b58\u5728\u4e00\u4e9b\u7279\u6b8a\u503c\uff0c\u5982\u65e0\u7a77\u5927\uff08<code>inf<\/code>\uff09\u548c\u975e\u6570\uff08<code>nan<\/code>\uff09\u3002\u8fd9\u4e9b\u7279\u6b8a\u503c\u53ef\u4ee5\u901a\u8fc7 <code>math<\/code> \u6a21\u5757\u8fdb\u884c\u5224\u65ad\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>b = float(&#39;inf&#39;)<\/p>\n<p>print(math.isinf(b))  # \u8f93\u51fa True<\/p>\n<p>print(isinstance(b, float))  # \u8f93\u51fa True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u590d\u6570\uff08complex\uff09\u7c7b\u578b\u5224\u65ad<\/h3>\n<\/p>\n<p><h4>1. \u590d\u6570\u7684\u7ec4\u6210<\/h4>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u590d\u6570\u7531\u5b9e\u90e8\u548c\u865a\u90e8\u7ec4\u6210\uff0c\u5f62\u5f0f\u4e3a <code>a + bj<\/code>\u3002\u53ef\u4ee5\u4f7f\u7528 <code>isinstance()<\/code> \u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u662f\u590d\u6570\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">z = 3 + 4j<\/p>\n<p>print(isinstance(z, complex))  # \u8f93\u51fa True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u590d\u6570\u7684\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u590d\u6570\u652f\u6301\u57fa\u672c\u7684\u6570\u5b66\u8fd0\u7b97\uff0c\u5982\u52a0\u3001\u51cf\u3001\u4e58\u3001\u9664\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 <code>abs()<\/code> \u8ba1\u7b97\u590d\u6570\u7684\u6a21\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">z1 = 1 + 2j<\/p>\n<p>z2 = 2 + 3j<\/p>\n<p>z3 = z1 + z2<\/p>\n<p>print(z3)  # \u8f93\u51fa (3+5j)<\/p>\n<p>print(abs(z1))  # \u8f93\u51fa 2.23606797749979<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5224\u65ad Python \u6570\u503c\u7c7b\u578b\u7684\u6838\u5fc3\u65b9\u6cd5\u662f\u4f7f\u7528 <strong><code>isinstance()<\/code> \u51fd\u6570<\/strong>\uff0c\u5b83\u4e0d\u4ec5\u7b80\u5355\u6613\u7528\uff0c\u8fd8\u652f\u6301\u5b50\u7c7b\u5224\u65ad\u3002\u6b64\u5916\uff0c\u7406\u89e3\u4e0d\u540c\u6570\u503c\u7c7b\u578b\u7684\u7279\u6027\u548c\u8fb9\u754c\u60c5\u51b5\uff0c\u6709\u52a9\u4e8e\u5728\u7f16\u7a0b\u4e2d\u907f\u514d\u6f5c\u5728\u9519\u8bef\u3002\u901a\u8fc7\u5408\u7406\u5e94\u7528\u6570\u503c\u7c7b\u578b\u5224\u65ad\uff0c\u53ef\u4ee5\u589e\u5f3a\u4ee3\u7801\u7684\u9c81\u68d2\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bc6\u522b\u4e0d\u540c\u7684\u6570\u503c\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6570\u503c\u7c7b\u578b\u4e3b\u8981\u5305\u62ec\u6574\u6570\uff08int\uff09\u3001\u6d6e\u70b9\u6570\uff08float\uff09\u548c\u590d\u6570\uff08complex\uff09\u3002\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>type()<\/code>\u51fd\u6570\u6765\u5224\u65ad\u53d8\u91cf\u7684\u6570\u503c\u7c7b\u578b\u3002\u4f8b\u5982\uff0c<code>type(5)<\/code>\u4f1a\u8fd4\u56de<code>&lt;class &#39;int&#39;&gt;<\/code>\uff0c\u800c<code>type(3.14)<\/code>\u4f1a\u8fd4\u56de<code>&lt;class &#39;float&#39;&gt;<\/code>\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u8bc6\u522b\u53d8\u91cf\u7684\u5177\u4f53\u6570\u503c\u7c7b\u578b\u3002<\/p>\n<p><strong>Python\u4e2d\u7684\u6570\u503c\u7c7b\u578b\u662f\u5426\u652f\u6301\u8fd0\u7b97\uff1f<\/strong><br \/>Python\u7684\u6570\u503c\u7c7b\u578b\u4e4b\u95f4\u662f\u53ef\u4ee5\u8fdb\u884c\u8fd0\u7b97\u7684\u3002\u4f8b\u5982\uff0c\u6574\u6570\u548c\u6d6e\u70b9\u6570\u53ef\u4ee5\u8fdb\u884c\u52a0\u3001\u51cf\u3001\u4e58\u3001\u9664\u7b49\u57fa\u672c\u8fd0\u7b97\u3002\u8fd0\u7b97\u7684\u7ed3\u679c\u4f1a\u6839\u636e\u53c2\u4e0e\u8fd0\u7b97\u7684\u6570\u503c\u7c7b\u578b\u81ea\u52a8\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2a\u6574\u6570\u4e0e\u6d6e\u70b9\u6570\u76f8\u52a0\u65f6\uff0c\u7ed3\u679c\u4f1a\u662f\u4e00\u4e2a\u6d6e\u70b9\u6570\u3002\u8fd9\u79cd\u7075\u6d3b\u6027\u4f7f\u5f97Python\u5728\u6570\u503c\u8ba1\u7b97\u65b9\u9762\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u4e2d\u7684\u6570\u503c\u7c7b\u578b\u8f6c\u6362\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8fdb\u884c\u6570\u503c\u7c7b\u578b\u7684\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>int()<\/code>\u53ef\u4ee5\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u4f7f\u7528<code>float()<\/code>\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u6216\u6574\u6570\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u4e22\u5931\u7cbe\u5ea6\uff0c\u4f8b\u5982\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\u65f6\u5c0f\u6570\u90e8\u5206\u4f1a\u88ab\u622a\u65ad\u3002\u4e86\u89e3\u8fd9\u4e9b\u8f6c\u6362\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u503c\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001\u5224\u65ad Python \u6570\u503c\u7c7b\u578b\u7684\u65b9\u6cd5 \u5728 Python \u4e2d\uff0c\u6570\u503c\u7c7b\u578b\u4e3b\u8981\u5305\u62ec\u6574\u6570\uff08int\uff09\u3001\u6d6e\u70b9\u6570\uff08floa [&hellip;]","protected":false},"author":3,"featured_media":997558,"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\/997537"}],"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=997537"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/997537\/revisions"}],"predecessor-version":[{"id":997562,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/997537\/revisions\/997562"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/997558"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=997537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=997537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=997537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}