{"id":1081633,"date":"2025-01-08T12:41:19","date_gmt":"2025-01-08T04:41:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1081633.html"},"modified":"2025-01-08T12:41:21","modified_gmt":"2025-01-08T04:41:21","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8%e5%87%bd%e6%95%b0%e8%8e%b7%e5%8f%96%e6%95%b0%e6%8d%ae%e7%9a%84%e7%b1%bb%e5%9e%8b-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1081633.html","title":{"rendered":"python\u5982\u4f55\u7528\u51fd\u6570\u83b7\u53d6\u6570\u636e\u7684\u7c7b\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183547\/df579563-77c4-4d23-98f8-327589864af5.webp\" alt=\"python\u5982\u4f55\u7528\u51fd\u6570\u83b7\u53d6\u6570\u636e\u7684\u7c7b\u578b\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u83b7\u53d6\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528<code>type()<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>isinstance()<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>types<\/code>\u6a21\u5757\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u5c31\u662f<code>type()<\/code>\u51fd\u6570\u3002\u4e0b\u9762\u5c31<code>type()<\/code>\u51fd\u6570\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<code>type()<\/code>\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u5bf9\u8c61\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f7f\u7528\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u3001\u5217\u8868\u7b49\u5e38\u89c1\u7c7b\u578b\uff0c<code>type()<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u76f8\u5e94\u7684\u7c7b\u578b\u540d\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u786e\u5b9a\u6570\u636e\u7c7b\u578b\u662f\u7f16\u7a0b\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u90e8\u5206\uff0c\u4e86\u89e3\u6570\u636e\u7c7b\u578b\u6709\u52a9\u4e8e\u7f16\u5199\u66f4\u9ad8\u6548\u3001\u66f4\u53ef\u9760\u7684\u4ee3\u7801\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u83b7\u53d6\u6570\u636e\u7c7b\u578b\u7684\u51e0\u79cd\u65b9\u6cd5\u4ee5\u53ca\u5b83\u4eec\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>type()<\/code>\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\u5bf9\u8c61\u7684\u7c7b\u578b\u3002\u5176\u8bed\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u76f4\u63a5\u4f20\u5165\u4e00\u4e2a\u5bf9\u8c61\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>a = 10<\/p>\n<p>b = 3.14<\/p>\n<p>c = &quot;Hello&quot;<\/p>\n<p>d = [1, 2, 3]<\/p>\n<p>print(type(a))  # &lt;class &#39;int&#39;&gt;<\/p>\n<p>print(type(b))  # &lt;class &#39;float&#39;&gt;<\/p>\n<p>print(type(c))  # &lt;class &#39;str&#39;&gt;<\/p>\n<p>print(type(d))  # &lt;class &#39;list&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>type()<\/code>\u51fd\u6570\u7684\u4f18\u52bf\u5728\u4e8e\u7b80\u5355\u660e\u4e86\uff0c\u53ef\u4ee5\u5feb\u901f\u67e5\u770b\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u5728\u8c03\u8bd5\u548c\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u8fd9\u4e2a\u529f\u80fd\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>isinstance()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>isinstance()<\/code>\u51fd\u6570\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u4e00\u4e2a\u5df2\u77e5\u7684\u7c7b\u578b\u3002\u8fd9\u4e2a\u51fd\u6570\u5bf9\u4e8e\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\u548c\u7c7b\u578b\u5224\u65ad\u7684\u573a\u666f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>a = 10<\/p>\n<p>b = 3.14<\/p>\n<p>c = &quot;Hello&quot;<\/p>\n<p>d = [1, 2, 3]<\/p>\n<p>print(isinstance(a, int))      # True<\/p>\n<p>print(isinstance(b, float))    # True<\/p>\n<p>print(isinstance(c, str))      # True<\/p>\n<p>print(isinstance(d, list))     # True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0e<code>type()<\/code>\u51fd\u6570\u76f8\u6bd4\uff0c<code>isinstance()<\/code>\u51fd\u6570\u66f4\u9002\u5408\u7528\u4e8e\u7c7b\u578b\u68c0\u67e5\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u68c0\u67e5\u662f\u5426\u662f\u67d0\u79cd\u7c7b\u578b\u7684\u5b50\u7c7b\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>types<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>types<\/code>\u6a21\u5757\u5305\u542b\u4e86Python\u4e2d\u6240\u6709\u6807\u51c6\u7c7b\u578b\u7684\u5b9a\u4e49\u3002\u901a\u8fc7\u8fd9\u4e2a\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u4e00\u4e9b\u7279\u6b8a\u7c7b\u578b\u8fdb\u884c\u68c0\u67e5\u3002<\/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(type(func))  # &lt;class &#39;function&#39;&gt;<\/p>\n<p>print(isinstance(func, types.FunctionType))  # True<\/p>\n<h2><strong>\u68c0\u67e5\u751f\u6210\u5668<\/strong><\/h2>\n<p>def generator():<\/p>\n<p>    yield 1<\/p>\n<p>gen = generator()<\/p>\n<p>print(type(gen))  # &lt;class &#39;generator&#39;&gt;<\/p>\n<p>print(isinstance(gen, types.GeneratorType))  # True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>types<\/code>\u6a21\u5757\u53ef\u4ee5\u66f4\u7cbe\u786e\u5730\u68c0\u67e5\u4e00\u4e9b\u7279\u6b8a\u7c7b\u578b\uff0c\u5982\u51fd\u6570\u3001\u751f\u6210\u5668\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u81ea\u5b9a\u4e49\u7c7b\u578b\u68c0\u67e5\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u5b9a\u4e49\u7c7b\u578b\u68c0\u67e5\u51fd\u6570\uff0c\u4ee5\u4fbf\u66f4\u7075\u6d3b\u5730\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def check_type(variable):<\/p>\n<p>    if isinstance(variable, int):<\/p>\n<p>        return &quot;Integer&quot;<\/p>\n<p>    elif isinstance(variable, float):<\/p>\n<p>        return &quot;Float&quot;<\/p>\n<p>    elif isinstance(variable, str):<\/p>\n<p>        return &quot;String&quot;<\/p>\n<p>    elif isinstance(variable, list):<\/p>\n<p>        return &quot;List&quot;<\/p>\n<p>    else:<\/p>\n<p>        return &quot;Unknown type&quot;<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>a = 10<\/p>\n<p>b = 3.14<\/p>\n<p>c = &quot;Hello&quot;<\/p>\n<p>d = [1, 2, 3]<\/p>\n<p>print(check_type(a))  # Integer<\/p>\n<p>print(check_type(b))  # Float<\/p>\n<p>print(check_type(c))  # String<\/p>\n<p>print(check_type(d))  # List<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u81ea\u5b9a\u4e49\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u6269\u5c55\u548c\u4fee\u6539\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u7c7b\u578b\u68c0\u67e5\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528<code>collections.abc<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5bb9\u5668\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>collections.abc<\/code>\u6a21\u5757\u4e2d\u7684\u62bd\u8c61\u57fa\u7c7b\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections.abc import Iterable, Iterator<\/p>\n<p>def check_iterable(variable):<\/p>\n<p>    if isinstance(variable, Iterable):<\/p>\n<p>        return &quot;Iterable&quot;<\/p>\n<p>    else:<\/p>\n<p>        return &quot;Not Iterable&quot;<\/p>\n<p>def check_iterator(variable):<\/p>\n<p>    if isinstance(variable, Iterator):<\/p>\n<p>        return &quot;Iterator&quot;<\/p>\n<p>    else:<\/p>\n<p>        return &quot;Not Iterator&quot;<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>a = [1, 2, 3]<\/p>\n<p>b = iter(a)<\/p>\n<p>print(check_iterable(a))  # Iterable<\/p>\n<p>print(check_iterable(b))  # Iterable<\/p>\n<p>print(check_iterator(a))  # Not Iterator<\/p>\n<p>print(check_iterator(b))  # Iterator<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>collections.abc<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5bf9\u5bb9\u5668\u7c7b\u578b\u8fdb\u884c\u66f4\u7cbe\u786e\u7684\u68c0\u67e5\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u83b7\u53d6\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u7279\u5b9a\u7684\u5e94\u7528\u573a\u666f\u3002<code>type()<\/code>\u51fd\u6570\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u5feb\u901f\u67e5\u770b\u53d8\u91cf\u7c7b\u578b\uff1b<code>isinstance()<\/code>\u51fd\u6570\u9002\u5408\u7528\u4e8e\u7c7b\u578b\u68c0\u67e5\uff0c\u5c24\u5176\u662f\u68c0\u67e5\u662f\u5426\u4e3a\u67d0\u79cd\u7c7b\u578b\u7684\u5b50\u7c7b\uff1b<code>types<\/code>\u6a21\u5757\u7528\u4e8e\u68c0\u67e5\u4e00\u4e9b\u7279\u6b8a\u7c7b\u578b\uff0c\u5982\u51fd\u6570\u3001\u751f\u6210\u5668\u7b49\uff1b\u81ea\u5b9a\u4e49\u7c7b\u578b\u68c0\u67e5\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u6269\u5c55\u548c\u4fee\u6539\uff1b<code>collections.abc<\/code>\u6a21\u5757\u9002\u7528\u4e8e\u68c0\u67e5\u590d\u6742\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5bb9\u5668\u7c7b\u578b\u3002<strong>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u9760\u6027\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u68c0\u67e5\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\u83b7\u53d6\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u53d8\u91cf<code>x = 10<\/code>\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>type(x)<\/code>\u6765\u83b7\u53d6<code>x<\/code>\u7684\u7c7b\u578b\uff0c\u8fd4\u56de\u7ed3\u679c\u5c06\u662f<code>&lt;class &#39;int&#39;&gt;<\/code>\uff0c\u8868\u793a<code>x<\/code>\u662f\u4e00\u4e2a\u6574\u6570\u7c7b\u578b\u3002<\/p>\n<p><strong>\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u4e3a\u7279\u5b9a\u7684\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>type()<\/code>\u51fd\u6570\u5916\uff0cPython\u8fd8\u63d0\u4f9b\u4e86<code>isinstance()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u5c5e\u4e8e\u67d0\u4e2a\u7279\u5b9a\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c<code>isinstance(x, int)<\/code>\u5c06\u8fd4\u56de<code>True<\/code>\uff0c\u5982\u679c<code>x<\/code>\u662f\u6574\u6570\u7c7b\u578b\u3002\u8fd9\u79cd\u65b9\u6cd5\u66f4\u4e3a\u7075\u6d3b\uff0c\u9002\u7528\u4e8e\u5224\u65ad\u5b50\u7c7b\u548c\u7236\u7c7b\u5173\u7cfb\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u67e5\u770b\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u7c7b\u578b\uff1f<\/strong><br \/>\u5bf9\u4e8e\u81ea\u5b9a\u4e49\u7c7b\u7684\u5bf9\u8c61\uff0c\u4f7f\u7528<code>type()<\/code>\u51fd\u6570\u540c\u6837\u6709\u6548\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u7c7b<code>Person<\/code>\uff0c\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61<code>p = Person()<\/code>\uff0c\u8c03\u7528<code>type(p)<\/code>\u5c06\u8fd4\u56de<code>&lt;class &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__.Person&#39;&gt;<\/code>\uff0c\u8fd9\u8868\u660e<code>p<\/code>\u662f<code>Person<\/code>\u7c7b\u7684\u5b9e\u4f8b\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u67e5\u770b\u4efb\u4f55\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u83b7\u53d6\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528type()\u51fd\u6570\u3001\u4f7f\u7528isinstance()\u51fd\u6570\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1081643,"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\/1081633"}],"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=1081633"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1081633\/revisions"}],"predecessor-version":[{"id":1081644,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1081633\/revisions\/1081644"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1081643"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1081633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1081633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1081633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}