{"id":970019,"date":"2024-12-27T05:22:16","date_gmt":"2024-12-26T21:22:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/970019.html"},"modified":"2024-12-27T05:22:18","modified_gmt":"2024-12-26T21:22:18","slug":"python%e5%a6%82%e4%bd%95%e7%a1%ae%e5%ae%9a%e5%ad%97%e6%ae%b5%e5%b1%9e%e6%80%a7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/970019.html","title":{"rendered":"python\u5982\u4f55\u786e\u5b9a\u5b57\u6bb5\u5c5e\u6027"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24184026\/dc4e3703-f272-471a-91cd-dbca6a43b78f.webp\" alt=\"python\u5982\u4f55\u786e\u5b9a\u5b57\u6bb5\u5c5e\u6027\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u786e\u5b9a\u5b57\u6bb5\u5c5e\u6027\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>type()<\/code>\u3001\u4f7f\u7528<code>isinstance()<\/code>\u51fd\u6570\u3001\u901a\u8fc7\u53cd\u5c04\u673a\u5236\u83b7\u53d6\u5c5e\u6027\u548c\u65b9\u6cd5\u3001\u5229\u7528\u6570\u636e\u7c7b\u578b\u7279\u5b9a\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>type()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u5b57\u6bb5\u7684\u6570\u636e\u7c7b\u578b\u3002<code>type()<\/code>\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u8be5\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u8fd9\u5bf9\u4e8e\u5feb\u901f\u68c0\u67e5\u53d8\u91cf\u7c7b\u578b\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5177\u4f53\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>type()<\/code><\/h3>\n<\/p>\n<p><p><code>type()<\/code>\u51fd\u6570\u662fPython\u4e2d\u7528\u4e8e\u83b7\u53d6\u5bf9\u8c61\u7c7b\u578b\u7684\u5185\u7f6e\u51fd\u6570\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u7c7b\u578b\u68c0\u6d4b\uff0c<code>type()<\/code>\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>type()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u53d8\u91cf\u6216\u5b57\u6bb5\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 10<\/p>\n<p>print(type(number))  # \u8f93\u51fa\uff1a&lt;class &#39;int&#39;&gt;<\/p>\n<p>text = &quot;Hello, World!&quot;<\/p>\n<p>print(type(text))  # \u8f93\u51fa\uff1a&lt;class &#39;str&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5224\u65ad\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>\u867d\u7136<code>type()<\/code>\u53ef\u4ee5\u7528\u4e8e\u68c0\u67e5\u7c7b\u578b\uff0c\u4f46\u5982\u679c\u9700\u8981\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u4e3a\u67d0\u79cd\u7c7b\u578b\uff0c\u901a\u5e38\u4f1a\u7ed3\u5408\u6bd4\u8f83\u8fd0\u7b97\u7b26<code>==<\/code>\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if type(number) == int:<\/p>\n<p>    print(&quot;number\u662f\u4e00\u4e2a\u6574\u6570\u3002&quot;)<\/p>\n<p><\/code><\/pre>\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\u68c0\u67e5\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u67d0\u4e2a\u7c7b\u7684\u5b9e\u4f8b\uff0c\u6216\u8005\u662f\u67d0\u4e2a\u7c7b\u7684\u5b50\u7c7b\u7684\u5b9e\u4f8b\u3002\u4e0e<code>type()<\/code>\u76f8\u6bd4\uff0c<code>isinstance()<\/code>\u66f4\u4e3a\u7075\u6d3b\uff0c\u5c24\u5176\u662f\u5728\u6d89\u53ca\u7ee7\u627f\u5173\u7cfb\u65f6\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>isinstance()<\/code>\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f\u5bf9\u8c61\uff0c\u7b2c\u4e8c\u4e2a\u662f\u7c7b\u6216\u7c7b\u578b\u7684\u5143\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 10<\/p>\n<p>if isinstance(number, int):<\/p>\n<p>    print(&quot;number\u662f\u4e00\u4e2a\u6574\u6570\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u68c0\u67e5\u591a\u4e2a\u7c7b\u578b<\/h4>\n<\/p>\n<p><p><code>isinstance()<\/code>\u53ef\u4ee5\u68c0\u67e5\u591a\u4e2a\u7c7b\u578b\uff0c\u53ea\u9700\u5c06\u7c7b\u578b\u653e\u5165\u4e00\u4e2a\u5143\u7ec4\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 3.14<\/p>\n<p>if isinstance(value, (int, float)):<\/p>\n<p>    print(&quot;value\u662f\u4e00\u4e2a\u6574\u6570\u6216\u6d6e\u70b9\u6570\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u53cd\u5c04\u673a\u5236\u83b7\u53d6\u5c5e\u6027\u548c\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u7684\u53cd\u5c04\u673a\u5236\u5141\u8bb8\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u68c0\u67e5\u548c\u4fee\u6539\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<code>getattr()<\/code>\u3001<code>hasattr()<\/code>\u3001<code>setattr()<\/code>\u7b49\u51fd\u6570\u662f\u5e38\u7528\u7684\u53cd\u5c04\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528<code>getattr()<\/code>\u548c<code>hasattr()<\/code><\/h4>\n<\/p>\n<p><p><code>getattr()<\/code>\u7528\u4e8e\u83b7\u53d6\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\uff0c<code>hasattr()<\/code>\u7528\u4e8e\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u6709\u67d0\u4e2a\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Sample:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.attribute = &quot;Sample Attribute&quot;<\/p>\n<p>sample = Sample()<\/p>\n<h2><strong>\u68c0\u67e5\u5c5e\u6027\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if hasattr(sample, &#39;attribute&#39;):<\/p>\n<p>    # \u83b7\u53d6\u5c5e\u6027\u503c<\/p>\n<p>    attr_value = getattr(sample, &#39;attribute&#39;)<\/p>\n<p>    print(f&quot;\u5c5e\u6027\u503c\u4e3a: {attr_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u52a8\u6001\u8bbe\u7f6e\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>setattr()<\/code>\u53ef\u4ee5\u52a8\u6001\u8bbe\u7f6e\u5bf9\u8c61\u7684\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u52a8\u6001\u6dfb\u52a0\u5c5e\u6027<\/p>\n<p>setattr(sample, &#39;new_attribute&#39;, &#39;New Value&#39;)<\/p>\n<p>print(sample.new_attribute)  # \u8f93\u51fa\uff1aNew Value<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5229\u7528\u6570\u636e\u7c7b\u578b\u7279\u5b9a\u7684\u5c5e\u6027\u548c\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u7684\u6bcf\u79cd\u6570\u636e\u7c7b\u578b\u90fd\u6709\u5176\u7279\u5b9a\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u6765\u8fdb\u4e00\u6b65\u786e\u5b9a\u5b57\u6bb5\u5c5e\u6027\u548c\u64cd\u4f5c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u5217\u8868\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u662f\u4e00\u79cd\u53ef\u53d8\u7684\u5e8f\u5217\u7c7b\u578b\uff0c\u5e38\u7528\u65b9\u6cd5\u5305\u62ec<code>append()<\/code>\u3001<code>extend()<\/code>\u3001<code>pop()<\/code>\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3]<\/p>\n<p>my_list.append(4)  # \u6dfb\u52a0\u5143\u7d204<\/p>\n<p>print(my_list)  # \u8f93\u51fa\uff1a[1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5b57\u5178\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u952e\u503c\u5bf9\u7684\u96c6\u5408\uff0c\u5e38\u7528\u65b9\u6cd5\u6709<code>keys()<\/code>\u3001<code>values()<\/code>\u3001<code>items()<\/code>\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>keys = my_dict.keys()<\/p>\n<p>print(keys)  # \u8f93\u51fa\uff1adict_keys([&#39;a&#39;, &#39;b&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5b57\u7b26\u4e32\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u53d8\u7684\u5e8f\u5217\u7c7b\u578b\uff0c\u5e38\u7528\u65b9\u6cd5\u5305\u62ec<code>upper()<\/code>\u3001<code>lower()<\/code>\u3001<code>split()<\/code>\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_string = &quot;hello&quot;<\/p>\n<p>print(my_string.upper())  # \u8f93\u51fa\uff1aHELLO<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u786e\u5b9a\u5b57\u6bb5\u5c5e\u6027\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c<strong>\u5408\u7406\u9009\u62e9\u5de5\u5177\u548c\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u8bfb\u6027<\/strong>\u3002<code>type()<\/code>\u548c<code>isinstance()<\/code>\u662f\u5224\u65ad\u7c7b\u578b\u7684\u57fa\u672c\u5de5\u5177\uff0c\u800c\u53cd\u5c04\u673a\u5236\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u52a8\u6001\u64cd\u4f5c\u80fd\u529b\u3002\u4e86\u89e3\u548c\u4f7f\u7528\u6570\u636e\u7c7b\u578b\u7279\u5b9a\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u64cd\u4f5c\u548c\u7406\u89e3Python\u4e2d\u7684\u5b57\u6bb5\u5c5e\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u5bf9\u8c61\u7684\u5c5e\u6027\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>type()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u4e00\u4e2a\u5b57\u6bb5\u7684\u5c5e\u6027\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u7c7b\u7684\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u901a\u8fc7<code>type(instance.field_name)<\/code>\u6765\u83b7\u53d6\u8be5\u5b57\u6bb5\u7684\u7c7b\u578b\u3002\u8fd9\u5bf9\u4e8e\u8c03\u8bd5\u548c\u7406\u89e3\u5bf9\u8c61\u7684\u7ed3\u6784\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u52a8\u6001\u83b7\u53d6\u7c7b\u7684\u5b57\u6bb5\u4fe1\u606f\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>vars()<\/code>\u51fd\u6570\u6216<code>__dict__<\/code>\u5c5e\u6027\u6765\u83b7\u53d6\u7c7b\u7684\u5b57\u6bb5\u4fe1\u606f\u3002<code>vars(instance)<\/code>\u5c06\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u53ca\u5176\u5bf9\u5e94\u7684\u503c\u3002\u8fd9\u6837\u4f60\u53ef\u4ee5\u8f7b\u677e\u67e5\u770b\u548c\u786e\u8ba4\u5b57\u6bb5\u7684\u5c5e\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u9a8c\u8bc1\u5b57\u6bb5\u7684\u503c\u662f\u5426\u7b26\u5408\u7279\u5b9a\u7c7b\u578b\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>isinstance()<\/code>\u51fd\u6570\u6765\u9a8c\u8bc1\u4e00\u4e2a\u5b57\u6bb5\u7684\u503c\u662f\u5426\u662f\u67d0\u79cd\u7279\u5b9a\u7c7b\u578b\u3002\u8fd9\u5bf9\u4e8e\u786e\u4fdd\u6570\u636e\u7684\u6709\u6548\u6027\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c<code>isinstance(instance.field_name, expected_type)<\/code>\u5c06\u8fd4\u56deTrue\u6216False\uff0c\u5e2e\u52a9\u4f60\u786e\u8ba4\u5b57\u6bb5\u7684\u503c\u662f\u5426\u7b26\u5408\u9884\u671f\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u786e\u5b9a\u5b57\u6bb5\u5c5e\u6027\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570type()\u3001\u4f7f\u7528isinstance()\u51fd\u6570\u3001\u901a\u8fc7\u53cd [&hellip;]","protected":false},"author":3,"featured_media":970023,"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\/970019"}],"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=970019"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/970019\/revisions"}],"predecessor-version":[{"id":970025,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/970019\/revisions\/970025"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/970023"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=970019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=970019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=970019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}