{"id":1179665,"date":"2025-01-15T18:26:49","date_gmt":"2025-01-15T10:26:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1179665.html"},"modified":"2025-01-15T18:26:51","modified_gmt":"2025-01-15T10:26:51","slug":"python%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1179665.html","title":{"rendered":"python\u5982\u4f55\u663e\u793a\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\/25114034\/80e4b5c0-99cf-4a3a-b2b3-c958ae64ef97.webp\" alt=\"python\u5982\u4f55\u663e\u793a\u6570\u636e\u7c7b\u578b\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u663e\u793a\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528type()\u51fd\u6570\u3001isinstance()\u51fd\u6570\u3001\u67e5\u770b\u53d8\u91cf\u7684\u5c5e\u6027\u4ee5\u53ca\u4f7f\u7528\u6a21\u5757\u67e5\u770b\u8be6\u7ec6\u4fe1\u606f\u3002<\/strong>\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u7f16\u5199\u4ee3\u7801\u65f6\u4e86\u89e3\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>type()<\/code>\u51fd\u6570\u3002<code>type()<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f7f\u7528\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5c06\u53d8\u91cf\u540d\u653e\u5165<code>type()<\/code>\u51fd\u6570\u4e2d\uff0c\u5373\u53ef\u5f97\u5230\u8be5\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<code>type(variable)<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u5feb\u901f\u68c0\u67e5\u53d8\u91cf\u7c7b\u578b\uff0c\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u4e2d\u663e\u793a\u6570\u636e\u7c7b\u578b\u7684\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001type()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>type()<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u51fd\u6570\u4e4b\u4e00\uff0c\u7528\u4e8e\u8fd4\u56de\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u4f7f\u7528\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5c06\u53d8\u91cf\u540d\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>type()<\/code>\u51fd\u6570\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>a = 10<\/p>\n<p>print(type(a))  # \u8f93\u51fa\uff1a&lt;class &#39;int&#39;&gt;<\/p>\n<p>b = 3.14<\/p>\n<p>print(type(b))  # \u8f93\u51fa\uff1a&lt;class &#39;float&#39;&gt;<\/p>\n<p>c = &quot;Hello, World!&quot;<\/p>\n<p>print(type(c))  # \u8f93\u51fa\uff1a&lt;class &#39;str&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u9762\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u53ef\u4ee5\u770b\u5230<code>type()<\/code>\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u53d8\u91cf\u7684\u5177\u4f53\u7c7b\u578b\uff0c\u5982\u6574\u6570\uff08int\uff09\u3001\u6d6e\u70b9\u6570\uff08float\uff09\u3001\u5b57\u7b26\u4e32\uff08str\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001isinstance()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>isinstance()<\/code>\u51fd\u6570\u7528\u4e8e\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u4e3a\u6307\u5b9a\u7c7b\u578b\u6216\u7c7b\u578b\u7684\u5b50\u7c7b\u3002\u8be5\u51fd\u6570\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f\u8981\u68c0\u67e5\u7684\u5bf9\u8c61\uff0c\u7b2c\u4e8c\u4e2a\u662f\u7c7b\u578b\u3002\u8fd4\u56de\u503c\u4e3a\u5e03\u5c14\u503c\uff08True\u6216False\uff09\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>a = 10<\/p>\n<p>print(isinstance(a, int))  # \u8f93\u51fa\uff1aTrue<\/p>\n<p>b = 3.14<\/p>\n<p>print(isinstance(b, float))  # \u8f93\u51fa\uff1aTrue<\/p>\n<p>c = &quot;Hello, World!&quot;<\/p>\n<p>print(isinstance(c, str))  # \u8f93\u51fa\uff1aTrue<\/p>\n<p>d = [1, 2, 3]<\/p>\n<p>print(isinstance(d, list))  # \u8f93\u51fa\uff1aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u9762\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u53ef\u4ee5\u770b\u5230<code>isinstance()<\/code>\u51fd\u6570\u53ef\u4ee5\u5224\u65ad\u53d8\u91cf\u662f\u5426\u4e3a\u67d0\u79cd\u7c7b\u578b\u6216\u7c7b\u578b\u7684\u5b50\u7c7b\u3002\u5982\u679c\u9700\u8981\u540c\u65f6\u68c0\u67e5\u591a\u4e2a\u7c7b\u578b\uff0c\u53ef\u4ee5\u5c06\u7c7b\u578b\u4f5c\u4e3a\u5143\u7ec4\u4f20\u9012\u7ed9<code>isinstance()<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>a = 10<\/p>\n<p>print(isinstance(a, (int, float)))  # \u8f93\u51fa\uff1aTrue<\/p>\n<p>b = 3.14<\/p>\n<p>print(isinstance(b, (int, float)))  # \u8f93\u51fa\uff1aTrue<\/p>\n<p>c = &quot;Hello, World!&quot;<\/p>\n<p>print(isinstance(c, (int, float)))  # \u8f93\u51fa\uff1aFalse<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u67e5\u770b\u53d8\u91cf\u7684\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53d8\u91cf\u672c\u8d28\u4e0a\u662f\u5bf9\u8c61\uff0c\u5bf9\u8c61\u62e5\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u901a\u8fc7\u67e5\u770b\u5bf9\u8c61\u7684\u5c5e\u6027\uff0c\u4e5f\u53ef\u4ee5\u4e86\u89e3\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>a = 10<\/p>\n<p>print(a.__class__)  # \u8f93\u51fa\uff1a&lt;class &#39;int&#39;&gt;<\/p>\n<p>b = 3.14<\/p>\n<p>print(b.__class__)  # \u8f93\u51fa\uff1a&lt;class &#39;float&#39;&gt;<\/p>\n<p>c = &quot;Hello, World!&quot;<\/p>\n<p>print(c.__class__)  # \u8f93\u51fa\uff1a&lt;class &#39;str&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u9762\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u53ef\u4ee5\u770b\u5230\u901a\u8fc7\u8bbf\u95ee\u5bf9\u8c61\u7684<code>__class__<\/code>\u5c5e\u6027\uff0c\u4e5f\u80fd\u83b7\u53d6\u53d8\u91cf\u7684\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u6a21\u5757\u67e5\u770b\u8be6\u7ec6\u4fe1\u606f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u67e5\u770b\u53d8\u91cf\u7684\u8be6\u7ec6\u7c7b\u578b\u4fe1\u606f\uff0c\u7279\u522b\u662f\u590d\u6742\u6570\u636e\u7ed3\u6784\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6a21\u5757\u6765\u83b7\u53d6\u8be6\u7ec6\u4fe1\u606f\u3002\u4f8b\u5982<code>collections<\/code>\u6a21\u5757\u4e2d\u7684<code>Counter<\/code>\u7c7b\u3001<code>defaultdict<\/code>\u7c7b\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>from collections import Counter, defaultdict<\/p>\n<p>e = Counter([1, 2, 3, 1, 2, 1])<\/p>\n<p>print(type(e))  # \u8f93\u51fa\uff1a&lt;class &#39;collections.Counter&#39;&gt;<\/p>\n<p>print(e.__class__)  # \u8f93\u51fa\uff1a&lt;class &#39;collections.Counter&#39;&gt;<\/p>\n<p>f = defaultdict(int)<\/p>\n<p>print(type(f))  # \u8f93\u51fa\uff1a&lt;class &#39;collections.defaultdict&#39;&gt;<\/p>\n<p>print(f.__class__)  # \u8f93\u51fa\uff1a&lt;class &#39;collections.defaultdict&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u9762\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u53ef\u4ee5\u770b\u5230\u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u7c7b\u65f6\uff0c<code>type()<\/code>\u51fd\u6570\u548c<code>__class__<\/code>\u5c5e\u6027\u90fd\u53ef\u4ee5\u83b7\u53d6\u8be6\u7ec6\u7684\u7c7b\u578b\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u81ea\u5b9a\u4e49\u7c7b\u548c\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u81ea\u5b9a\u4e49\u7c7b\u548c\u521b\u5efa\u5bf9\u8c61\u3002\u81ea\u5b9a\u4e49\u7c7b\u548c\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>type()<\/code>\u51fd\u6570\u3001<code>isinstance()<\/code>\u51fd\u6570\u548c\u67e5\u770b\u5c5e\u6027\u7684\u65b9\u6cd5\u6765\u68c0\u67e5\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>class MyClass:<\/p>\n<p>    pass<\/p>\n<p>g = MyClass()<\/p>\n<p>print(type(g))  # \u8f93\u51fa\uff1a&lt;class &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__.MyClass&#39;&gt;<\/p>\n<p>print(g.__class__)  # \u8f93\u51fa\uff1a&lt;class &#39;__main__.MyClass&#39;&gt;<\/p>\n<p>print(isinstance(g, MyClass))  # \u8f93\u51fa\uff1aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u9762\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u53ef\u4ee5\u770b\u5230\u81ea\u5b9a\u4e49\u7c7b\u548c\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u68c0\u67e5\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u663e\u793a\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u4f7f\u7528<code>type()<\/code>\u51fd\u6570\u3001<code>isinstance()<\/code>\u51fd\u6570\u3001\u67e5\u770b\u53d8\u91cf\u7684\u5c5e\u6027\u4ee5\u53ca\u4f7f\u7528\u6a21\u5757\u67e5\u770b\u8be6\u7ec6\u4fe1\u606f\u3002<strong><code>type()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5feb\u901f\u68c0\u67e5\u53d8\u91cf\u7c7b\u578b\u3002<\/strong> <code>isinstance()<\/code>\u51fd\u6570\u7528\u4e8e\u5224\u65ad\u5bf9\u8c61\u662f\u5426\u4e3a\u6307\u5b9a\u7c7b\u578b\u6216\u5176\u5b50\u7c7b\u3002\u67e5\u770b\u53d8\u91cf\u7684\u5c5e\u6027\u53ef\u4ee5\u83b7\u53d6\u5bf9\u8c61\u7684\u7c7b\u578b\u4fe1\u606f\u3002\u4f7f\u7528\u6a21\u5757\u53ef\u4ee5\u83b7\u53d6\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u8be6\u7ec6\u7c7b\u578b\u4fe1\u606f\u3002\u81ea\u5b9a\u4e49\u7c7b\u548c\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u68c0\u67e5\u7c7b\u578b\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u65b9\u4fbf\u5730\u4e86\u89e3\u53d8\u91cf\u7684\u7c7b\u578b\uff0c\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u67e5\u770b\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>type()<\/code>\u51fd\u6570\u6765\u67e5\u770b\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u53d8\u91cf<code>x = 5<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>print(type(x))<\/code>\u6765\u8f93\u51fa\u5176\u6570\u636e\u7c7b\u578b\uff0c\u8fd9\u5c06\u8fd4\u56de<code>&lt;class &#39;int&#39;&gt;<\/code>\uff0c\u8868\u793a\u8fd9\u662f\u4e00\u4e2a\u6574\u6570\u7c7b\u578b\u3002<\/p>\n<p><strong>Python\u652f\u6301\u54ea\u4e9b\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6574\u6570\uff08int\uff09\u3001\u6d6e\u70b9\u6570\uff08float\uff09\u3001\u5b57\u7b26\u4e32\uff08str\uff09\u3001\u5e03\u5c14\u503c\uff08bool\uff09\u7b49\u3002\u4e86\u89e3\u8fd9\u4e9b\u6570\u636e\u7c7b\u578b\u6709\u52a9\u4e8e\u5728\u7f16\u5199\u4ee3\u7801\u65f6\u9009\u62e9\u5408\u9002\u7684\u7c7b\u578b\uff0c\u4ee5\u4fbf\u8fdb\u884c\u6709\u6548\u7684\u6570\u636e\u5904\u7406\u548c\u5b58\u50a8\u3002<\/p>\n<p><strong>\u5982\u4f55\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u5c5e\u4e8e\u540c\u4e00\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>isinstance()<\/code>\u51fd\u6570\u6765\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u5c5e\u4e8e\u7279\u5b9a\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c<code>isinstance(x, int)<\/code>\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u53d8\u91cf<code>x<\/code>\u662f\u5426\u662f\u6574\u6570\u7c7b\u578b\u3002\u5982\u679c\u8fd4\u56de<code>True<\/code>\uff0c\u5219\u8bf4\u660e<code>x<\/code>\u7684\u786e\u662f\u6574\u6570\u7c7b\u578b\uff0c\u8fd9\u5728\u6570\u636e\u9a8c\u8bc1\u548c\u6761\u4ef6\u5224\u65ad\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u663e\u793a\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528type()\u51fd\u6570\u3001isinstance()\u51fd\u6570\u3001\u67e5\u770b\u53d8\u91cf\u7684\u5c5e\u6027\u4ee5\u53ca\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1179670,"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\/1179665"}],"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=1179665"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179665\/revisions"}],"predecessor-version":[{"id":1179673,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179665\/revisions\/1179673"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1179670"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1179665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1179665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1179665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}