{"id":174290,"date":"2024-05-08T18:28:48","date_gmt":"2024-05-08T10:28:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/174290.html"},"modified":"2024-05-08T18:28:55","modified_gmt":"2024-05-08T10:28:55","slug":"python3%e4%b8%adtype%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/174290.html","title":{"rendered":"Python3\u4e2dtype\u5982\u4f55\u8f93\u51fa\u6570\u636e\u7c7b\u578b"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27050449\/700a8721-708f-415b-bb9d-0a34ca3957f6.webp\" alt=\"Python3\u4e2dtype\u5982\u4f55\u8f93\u51fa\u6570\u636e\u7c7b\u578b\" \/><\/p>\n<p><p>\u5728Python3\u4e2d\uff0c\u4f7f\u7528<code>type()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f93\u51fa\u4e00\u4e2a\u5bf9\u8c61\u7684\u6570\u636e\u7c7b\u578b\u3002\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\u3001\u8fd4\u56de\u7ed3\u679c\u662f\u8be5\u5bf9\u8c61\u6240\u5c5e\u7684\u7c7b\u578b\uff0c\u5982\u7c7b<code>&lt;class &#039;int&#039;&gt;<\/code>\u3001<code>&lt;class &#039;str&#039;&gt;<\/code>\u3001<code>&lt;class &#039;list&#039;&gt;<\/code>\u7b49\u3002<strong>\u5bf9\u4e8e\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u638c\u63e1type()\u51fd\u6570\u662f\u4e86\u89e3\u548c\u8c03\u8bd5\u4ee3\u7801\u4e2d\u53d8\u91cf\u7c7b\u578b\u7684\u57fa\u672c\u6280\u80fd\u4e4b\u4e00<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5f53\u4f60\u9700\u8981\u786e\u8ba4\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b\u65f6\uff0c\u4f60\u53ef\u4ee5\u5982\u4e0b\u4f7f\u7528<code>type()<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">variable = &quot;Hello, World!&quot;<\/p>\n<p>variable_type = type(variable)<\/p>\n<p>print(variable_type)  # \u8f93\u51fa: &lt;class &#039;str&#039;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>variable<\/code>\u88ab\u8d4b\u4e88\u4e86\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u800c<code>type(variable)<\/code>\u6b63\u662f\u7528\u6765\u83b7\u53d6\u8fd9\u4e2a\u5b57\u7b26\u4e32\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001<code>TYPE()<\/code>\u51fd\u6570\u7684\u5de5\u4f5c\u673a\u5236<\/h3>\n<\/p>\n<p><p><code>type()<\/code>\u662fPython\u7684\u4e00\u4e2a\u5185\u5efa\u51fd\u6570\uff0c\u5b83\u5728Python\u7684\u6838\u5fc3\u64cd\u4f5c\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u8be5\u51fd\u6570\u7684\u5de5\u4f5c\u8fc7\u7a0b\u975e\u5e38\u76f4\u63a5\u2014\u2014\u5b83\u67e5\u770b\u4f20\u9012\u7ed9\u5b83\u7684\u5bf9\u8c61\uff0c\u5e76\u8fd4\u56de\u8be5\u5bf9\u8c61\u7684\u7c7b\u578b\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>\u529f\u80fd\u8be6\u89e3<\/h4>\n<\/p>\n<p><p>\u5f53\u4f60\u4f20\u9012\u4e00\u4e2a\u5bf9\u8c61\u7ed9<code>type()<\/code>\u51fd\u6570\u65f6\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u8868\u793a\u5bf9\u8c61\u7c7b\u578b\u7684\u7c7b\u5bf9\u8c61\u3002\u5982\u679c\u5bf9\u8c61\u662f\u5185\u5efa\u7c7b\u578b\uff0c\u5982\u5217\u8868\u6216\u5b57\u5178\uff0c<code>type()<\/code>\u4f1a\u8fd4\u56dePython\u5185\u90e8\u9884\u5b9a\u4e49\u7684\u7c7b\u578b\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>\u4f8b\u5b50\u6f14\u793a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">number = 123<\/p>\n<p>text = &#039;example&#039;<\/p>\n<p>list_of_numbers = [1, 2, 3]<\/p>\n<p>type_of_number = type(number)<\/p>\n<p>type_of_text = type(text)<\/p>\n<p>type_of_list = type(list_of_numbers)<\/p>\n<p>print(type_of_number)  # \u8f93\u51fa: &lt;class &#039;int&#039;&gt;<\/p>\n<p>print(type_of_text)    # \u8f93\u51fa: &lt;class &#039;str&#039;&gt;<\/p>\n<p>print(type_of_list)    # \u8f93\u51fa: &lt;class &#039;list&#039;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5728\u5b9e\u8df5\u4e2d\u8fd0\u7528<code>TYPE()<\/code><\/h3>\n<\/p>\n<p><p><code>type()<\/code>\u51fd\u6570\u5728\u5f00\u53d1\u4e2d\u7684\u5b9e\u8df5\u7528\u9014\u662f\u591a\u65b9\u9762\u7684\u3002\u4ece\u7c7b\u578b\u68c0\u67e5\u5230\u6761\u4ef6\u903b\u8f91\uff0c\u518d\u5230\u53cd\u5c04\u548c\u52a8\u6001\u4ee3\u7801\u8fd0\u884c\uff0c<code>type()<\/code>\u90fd\u626e\u6f14\u7740\u5173\u952e\u89d2\u8272\u3002<\/p>\n<\/p>\n<p><h4>\u7c7b\u578b\u68c0\u67e5<\/h4>\n<\/p>\n<p><p>\u5728\u7f16\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u6839\u636e\u53d8\u91cf\u7684\u7c7b\u578b\u6765\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002<code>type()<\/code>\u5728\u8fd9\u65b9\u9762\u63d0\u4f9b\u4e86\u5de8\u5927\u5e2e\u52a9\uff0c\u56e0\u4e3a\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u786e\u5b9a\u53d8\u91cf\u7684\u7c7b\u578b\u662f\u5426\u6ee1\u8db3\u4f60\u7684\u8981\u6c42\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def process(input_data):<\/p>\n<p>    if type(input_data) is str:<\/p>\n<p>        print(&quot;\u5904\u7406\u5b57\u7b26\u4e32&quot;)<\/p>\n<p>    elif type(input_data) is int:<\/p>\n<p>        print(&quot;\u5904\u7406\u6574\u6570&quot;)<\/p>\n<p>    # \u5176\u4ed6\u7c7b\u578b\u5904\u7406\u7684\u4ee3\u7801...<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001<code>TYPE()<\/code>\u4e0e\u5176\u4ed6\u7c7b\u578b\u68c0\u67e5\u65b9\u5f0f\u7684\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p><code>type()<\/code>\u5e76\u4e0d\u662fPython\u4e2d\u68c0\u67e5\u7c7b\u578b\u7684\u552f\u4e00\u65b9\u5f0f\u3002<code>isinstance()<\/code>\u662f\u53e6\u4e00\u4e2a\u5e38\u7528\u7684\u51fd\u6570\uff0c\u5b83\u4e0d\u4ec5\u68c0\u67e5\u5bf9\u8c61\u7684\u76f4\u63a5\u7c7b\u578b\uff0c\u8fd8\u80fd\u68c0\u67e5\u5b83\u662f\u5426\u662f\u4e00\u4e2a\u7c7b\u7684\u5b50\u7c7b\u3002<\/p>\n<\/p>\n<p><h4>\u4e3a\u4f55\u9009\u62e9<code>TYPE()<\/code>\u6216<code>ISINSTANCE()<\/code><\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>type()<\/code>\u7684\u60c5\u51b5\u4e3b\u8981\u662f\u5f53\u4f60\u9700\u8981\u83b7\u53d6\u7c7b\u578b\u672c\u8eab\u7684\u4fe1\u606f\u65f6\uff0c\u800c<code>isinstance()<\/code>\u66f4\u9002\u5408\u7528\u4e8e\u7c7b\u578b\u7684\u6bd4\u8f83\u3002<code>isinstance()<\/code>\u8fd8\u5141\u8bb8\u6bd4\u8f83\u591a\u4e2a\u7c7b\u578b\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4ec5\u9700\u5224\u65ad\u5bf9\u8c61\u662f\u5426\u4e3a\u7279\u5b9a\u7684\u7c7b\u578b\uff0c\u800c\u4e0d\u5173\u5fc3\u5b83\u662f\u5426\u662f\u8be5\u7c7b\u578b\u7684\u5b50\u7c7b\uff0c\u90a3\u4e48<code>type()<\/code>\u662f\u4e00\u4e2a\u597d\u7684\u9009\u62e9\u3002\u5bf9\u4e8e\u66f4\u901a\u7528\u7684\u7c7b\u578b\u68c0\u67e5\uff0c<code>isinstance()<\/code>\u6216\u8bb8\u662f\u66f4\u4f73\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001<code>TYPE()<\/code>\u7684\u9ad8\u7ea7\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u66f4\u590d\u6742\u7684Python\u5e94\u7528\u4e2d\uff0c<code>type()<\/code>\u4e0d\u4ec5\u7528\u4e8e\u7c7b\u578b\u68c0\u67e5\u3002\u5b83\u540c\u6837\u80fd\u591f\u7528\u4e8e\u5143\u7f16\u7a0b\u2014\u2014\u7f16\u5199\u521b\u5efa\u6216\u64cd\u4f5c\u7c7b\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>\u52a8\u6001\u521b\u5efa\u7c7b<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>type()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u521b\u5efa\u7c7b\u3002\u8fd9\u662f\u4e00\u4e2a\u9ad8\u7ea7\u6280\u672f\uff0c\u901a\u5e38\u7528\u4e8e\u9700\u8981\u5927\u91cf\u52a8\u6001\u751f\u6210\u7c7b\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">MyClass = type(&#039;MyClass&#039;, (object,), {&#039;x&#039;: 5})<\/p>\n<h2><strong>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aMyClass\u7684\u7c7b\uff0c\u4eceobject\u7ee7\u627f\uff0c\u5e76\u62e5\u6709\u4e00\u4e2a\u7c7b\u5c5e\u6027x<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7c7b\u7684\u5143\u4fe1\u606f<\/h4>\n<\/p>\n<p><p><code>type()<\/code>\u53e6\u4e00\u4e2a\u7528\u9014\u662f\u83b7\u53d6\u5173\u4e8e\u7c7b\u7684\u5143\u4fe1\u606f\uff0c\u6bd4\u5982\u57fa\u7c7b\u6216\u540d\u79f0\u3002\u8fd9\u5728\u53cd\u5c04\u7f16\u7a0b\u4e2d\u5341\u5206\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><code>type()<\/code>\u51fd\u6570\u5728Python\u4e2d\u662f\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u7684\u5de5\u5177\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u5feb\u901f\u800c\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u83b7\u53d6\u548c\u68c0\u67e5\u6570\u636e\u7c7b\u578b\u3002\u65e0\u8bba\u662f\u65b0\u624b\u8fd8\u662f\u7ecf\u9a8c\u4e30\u5bcc\u7684\u5f00\u53d1\u8005\uff0c\u90fd\u4f1a\u5728\u65e5\u5e38\u7f16\u7801\u4e2d\u9891\u7e41\u4f7f\u7528\u5230<code>type()<\/code>\u3002\u5b83\u7684\u5e94\u7528\u8303\u56f4\u5e7f\u6cdb\uff0c\u4e0d\u4ec5\u9650\u4e8e\u7c7b\u578b\u68c0\u67e5\uff0c\u8fd8\u5305\u62ec\u7c7b\u7684\u521b\u5efa\u548c\u5143\u7f16\u7a0b\u7b49\u9ad8\u7ea7\u529f\u80fd\u3002\u5728\u7f16\u5199\u7075\u6d3b\u4e14\u5065\u58ee\u7684Python\u4ee3\u7801\u65f6\uff0c<code>type()<\/code>\u65e0\u7591\u662f\u4f60\u5de5\u5177\u7bb1\u4e2d\u7684\u4e00\u4e2a\u5b9d\u8d35\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528Python3\u7684type\u51fd\u6570\u6765\u83b7\u53d6\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\n\u8981\u83b7\u53d6\u6570\u636e\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528Python3\u5185\u7f6e\u7684type\u51fd\u6570\u3002\u901a\u8fc7\u5bf9\u53d8\u91cf\u6216\u503c\u8c03\u7528type\u51fd\u6570\uff0c\u5c06\u8fd4\u56de\u8be5\u53d8\u91cf\u6216\u503c\u7684\u6570\u636e\u7c7b\u578b\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">value = 19.5\nprint(type(value))  # \u8f93\u51fa\uff1a&lt;class &#039;float&#039;&gt;\uff0c\u8868\u793avalue\u7684\u6570\u636e\u7c7b\u578b\u662f\u6d6e\u70b9\u6570\n\ntext = &quot;Hello, World!&quot;\nprint(type(text))  # \u8f93\u51fa\uff1a&lt;class &#039;str&#039;&gt;\uff0c\u8868\u793atext\u7684\u6570\u636e\u7c7b\u578b\u662f\u5b57\u7b26\u4e32\n\nnumbers = [1, 2, 3, 4, 5]\nprint(type(numbers))  # \u8f93\u51fa\uff1a&lt;class &#039;list&#039;&gt;\uff0c\u8868\u793anumbers\u7684\u6570\u636e\u7c7b\u578b\u662f\u5217\u8868\n<\/code><\/pre>\n<p><strong>2. Python3\u4e2d\u7684type\u51fd\u6570\u6709\u4ec0\u4e48\u4f5c\u7528\uff1f<\/strong><br \/>\n\u5728\u7f16\u7a0b\u4e2d\uff0c\u6709\u65f6\u5019\u9700\u8981\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u6216\u503c\u7684\u6570\u636e\u7c7b\u578b\u3002\u5728Python3\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528type\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u7684\u3002\u901a\u8fc7\u8c03\u7528type\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5feb\u901f\u4e86\u89e3\u6570\u636e\u7684\u7c7b\u578b\u662f\u4ec0\u4e48\uff0c\u4ece\u800c\u66f4\u597d\u5730\u5904\u7406\u548c\u64cd\u4f5c\u6570\u636e\u3002\u4f8b\u5982\uff0c\u5f53\u9700\u8981\u5bf9\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u53d8\u91cf\u8fdb\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u65f6\uff0c\u4f7f\u7528type\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u7684\u5224\u65ad\u548c\u5206\u6d41\u5904\u7406\u3002<\/p>\n<p><strong>3. Python3\u4e2dtype\u51fd\u6570\u9002\u7528\u4e8e\u54ea\u4e9b\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\nPython3\u7684type\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u5404\u79cd\u6570\u636e\u7c7b\u578b\u7684\u4fe1\u606f\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a\u6574\u6570\uff08int\uff09\u3001\u6d6e\u70b9\u6570\uff08float\uff09\u3001\u5b57\u7b26\u4e32\uff08str\uff09\u3001\u5217\u8868\uff08list\uff09\u3001\u5143\u7ec4\uff08tuple\uff09\u3001\u5b57\u5178\uff08dict\uff09\u3001\u96c6\u5408\uff08set\uff09\u3001\u5e03\u5c14\u503c\uff08bool\uff09\u3001\u51fd\u6570\uff08function\uff09\u7b49\u3002\u4e0d\u4ec5\u5982\u6b64\uff0ctype\u51fd\u6570\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5224\u65ad\u81ea\u5b9a\u4e49\u7684\u7c7b\u6216\u5bf9\u8c61\u7684\u6570\u636e\u7c7b\u578b\u3002\u6240\u4ee5\uff0c\u65e0\u8bba\u662f\u5185\u7f6e\u6570\u636e\u7c7b\u578b\u8fd8\u662f\u81ea\u5b9a\u4e49\u6570\u636e\u7c7b\u578b\uff0c\u90fd\u53ef\u4ee5\u4f7f\u7528type\u51fd\u6570\u6765\u83b7\u53d6\u5176\u6570\u636e\u7c7b\u578b\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python3\u4e2d\uff0c\u4f7f\u7528type()\u51fd\u6570\u53ef\u4ee5\u8f93\u51fa\u4e00\u4e2a\u5bf9\u8c61\u7684\u6570\u636e\u7c7b\u578b\u3002\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\u3001\u8fd4\u56de\u7ed3\u679c\u662f\u8be5\u5bf9\u8c61\u6240\u5c5e\u7684 [&hellip;]","protected":false},"author":3,"featured_media":174302,"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\/174290"}],"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=174290"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/174290\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/174302"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=174290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=174290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=174290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}