{"id":972272,"date":"2024-12-27T05:47:13","date_gmt":"2024-12-26T21:47:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/972272.html"},"modified":"2024-12-27T05:47:15","modified_gmt":"2024-12-26T21:47:15","slug":"%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8bpython%e7%9a%84%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/972272.html","title":{"rendered":"\u5982\u4f55\u67e5\u770bpython\u7684\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195024\/0beee604-d076-412b-ba4f-e5a69971fd67.webp\" alt=\"\u5982\u4f55\u67e5\u770bpython\u7684\u53d8\u91cf\" \/><\/p>\n<p><p> <strong>\u67e5\u770bPython\u53d8\u91cf\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u3001\u5229\u7528<code>type()<\/code>\u51fd\u6570\u3001\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\u3001\u4f7f\u7528<code>dir()<\/code>\u51fd\u6570\u548c<code>vars()<\/code>\u51fd\u6570\u3002<\/strong>\u5176\u4e2d\uff0c<strong><code>print()<\/code>\u51fd\u6570<\/strong>\u662f\u6700\u5e38\u7528\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u8f93\u51fa\u53d8\u91cf\u7684\u503c\u5230\u63a7\u5236\u53f0\uff0c\u4ee5\u4fbf\u4e8e\u68c0\u67e5\u53d8\u91cf\u7684\u5f53\u524d\u72b6\u6001\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5feb\u901f\u5730\u786e\u8ba4\u53d8\u91cf\u662f\u5426\u5305\u542b\u9884\u671f\u7684\u6570\u636e\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u67e5\u770b\u53d8\u91cf<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>print()<\/code>\u51fd\u6570\u662f\u6700\u57fa\u672c\u7684\u8f93\u51fa\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u8c03\u7528<code>print()<\/code>\u51fd\u6570\u5e76\u5c06\u53d8\u91cf\u540d\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff0c\u7a0b\u5e8f\u4f1a\u5728\u63a7\u5236\u53f0\u663e\u793a\u8be5\u53d8\u91cf\u7684\u5f53\u524d\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u4e14\u76f4\u89c2\uff0c\u662f\u8c03\u8bd5Python\u4ee3\u7801\u65f6\u6700\u5e38\u7528\u7684\u624b\u6bb5\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_variable = 42<\/p>\n<p>print(my_variable)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u4ee3\u7801\u4f1a\u5728\u63a7\u5236\u53f0\u8f93\u51fa<code>42<\/code>\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u786e\u8ba4<code>my_variable<\/code>\u7684\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u68c0\u67e5\u7b80\u5355\u53d8\u91cf\u6216\u5728\u4ee3\u7801\u6267\u884c\u7684\u67d0\u4e2a\u7279\u5b9a\u70b9\u67e5\u770b\u53d8\u91cf\u7684\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5229\u7528<code>type()<\/code>\u51fd\u6570\u67e5\u770b\u53d8\u91cf\u7c7b\u578b<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u67e5\u770b\u53d8\u91cf\u7684\u503c\uff0c\u6709\u65f6\u6211\u4eec\u9700\u8981\u77e5\u9053\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u5728Python\u4e2d\uff0c<code>type()<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u53d8\u91cf\u7684\u7c7b\u578b\u4fe1\u606f\u3002\u901a\u8fc7\u8c03\u7528<code>type()<\/code>\u51fd\u6570\u5e76\u4f20\u5165\u53d8\u91cf\u540d\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u77e5\u9053\u53d8\u91cf\u662f\u6574\u6570\u3001\u5b57\u7b26\u4e32\u3001\u5217\u8868\u7b49\u54ea\u79cd\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_variable = 42<\/p>\n<p>print(type(my_variable))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa<code>&lt;class &#39;int&#39;&gt;<\/code>\uff0c\u8868\u793a<code>my_variable<\/code>\u662f\u4e00\u4e2a\u6574\u6570\u7c7b\u578b\u3002\u8fd9\u5bf9\u4e8e\u5904\u7406\u6570\u636e\u7c7b\u578b\u4e0d\u660e\u786e\u7684\u53d8\u91cf\u5c24\u5176\u6709\u7528\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u907f\u514d\u7c7b\u578b\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u8c03\u8bd5\u5de5\u5177\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u67e5\u770b\u548c\u4fee\u6539\u53d8\u91cf\u3002\u8fd9\u4e9b\u5de5\u5177\u5305\u62ecPython\u81ea\u5e26\u7684<code>pdb<\/code>\u6a21\u5757\uff0c\u4ee5\u53ca\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u3001VSCode\u7b49\u5185\u7f6e\u7684\u8c03\u8bd5\u5668\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528<code>pdb<\/code>\u6a21\u5757\uff1a<\/strong><\/p>\n<\/p>\n<p><p><code>pdb<\/code>\u662fPython\u7684\u6807\u51c6\u8c03\u8bd5\u5668\uff0c\u901a\u8fc7\u5728\u4ee3\u7801\u4e2d\u63d2\u5165\u65ad\u70b9\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u5e76\u5728\u6bcf\u4e2a\u6b65\u9aa4\u67e5\u770b\u548c\u4fee\u6539\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pdb<\/p>\n<p>my_variable = 42<\/p>\n<p>pdb.set_trace()<\/p>\n<p>print(my_variable)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5f53\u7a0b\u5e8f\u8fd0\u884c\u5230<code>pdb.set_trace()<\/code>\u65f6\uff0c\u4f1a\u8fdb\u5165\u8c03\u8bd5\u6a21\u5f0f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u67e5\u770b\u53d8\u91cf\u72b6\u6001\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528IDE\u8c03\u8bd5\u5668\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u73b0\u4ee3IDE\u901a\u5e38\u81ea\u5e26\u5f3a\u5927\u7684\u8c03\u8bd5\u529f\u80fd\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7\u5728\u4ee3\u7801\u884c\u4e0a\u8bbe\u7f6e\u65ad\u70b9\uff0c\u542f\u52a8\u8c03\u8bd5\u6a21\u5f0f\uff0c\u67e5\u770b\u53d8\u91cf\u7684\u503c\u548c\u7c7b\u578b\u3002\u8fd9\u79cd\u65b9\u6cd5\u76f4\u89c2\u4e14\u9ad8\u6548\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u8c03\u8bd5\u4efb\u52a1\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528<code>dir()<\/code>\u51fd\u6570\u67e5\u770b\u5bf9\u8c61\u5c5e\u6027<\/p>\n<\/p>\n<p><p><code>dir()<\/code>\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u6307\u5b9a\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u5217\u8868\uff0c\u5305\u62ec\u53d8\u91cf\u7684\u540d\u79f0\u3002\u5728\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u4f7f\u7528<code>dir()<\/code>\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u6d4f\u89c8\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    my_variable = 42<\/p>\n<p>obj = MyClass()<\/p>\n<p>print(dir(obj))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa<code>obj<\/code>\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u8fd9\u5bf9\u4e8e\u67e5\u770b\u5bf9\u8c61\u7684\u7ed3\u6784\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528<code>vars()<\/code>\u51fd\u6570\u67e5\u770b\u5bf9\u8c61\u7684<code>__dict__<\/code>\u5c5e\u6027<\/p>\n<\/p>\n<p><p><code>vars()<\/code>\u51fd\u6570\u8fd4\u56de\u5bf9\u8c61\u7684<code>__dict__<\/code>\u5c5e\u6027\uff0c\u5b83\u662f\u4e00\u4e2a\u5305\u542b\u5bf9\u8c61\u53ef\u5199\u5c5e\u6027\u7684\u5b57\u5178\u3002\u901a\u8fc7\u67e5\u770b<code>__dict__<\/code>\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u4e86\u89e3\u5bf9\u8c61\u7684\u5f53\u524d\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.my_variable = 42<\/p>\n<p>obj = MyClass()<\/p>\n<p>print(vars(obj))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u8f93\u51fa<code>{&#39;my_variable&#39;: 42}<\/code>\uff0c\u663e\u793a<code>obj<\/code>\u5bf9\u8c61\u7684\u6240\u6709\u5b9e\u4f8b\u53d8\u91cf\u53ca\u5176\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u67e5\u770b\u7c7b\u5b9e\u4f8b\u7684\u5185\u90e8\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u4ee5\u4e0a\u5185\u5bb9\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u67e5\u770b\u53d8\u91cf\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u63d0\u9ad8\u8c03\u8bd5\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u67e5\u770b\u6240\u6709\u5f53\u524d\u7684\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>locals()<\/code>\u548c<code>globals()<\/code>\u51fd\u6570\u6765\u67e5\u770b\u5f53\u524d\u4f5c\u7528\u57df\u4e2d\u7684\u6240\u6709\u53d8\u91cf\u3002<code>locals()<\/code>\u8fd4\u56de\u5f53\u524d\u5c40\u90e8\u4f5c\u7528\u57df\u7684\u53d8\u91cf\u5b57\u5178\uff0c\u800c<code>globals()<\/code>\u8fd4\u56de\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\u5b57\u5178\u3002\u60a8\u53ea\u9700\u5728\u547d\u4ee4\u884c\u6216\u811a\u672c\u4e2d\u8f93\u5165\u8fd9\u4e24\u4e2a\u51fd\u6570\uff0c\u5373\u53ef\u67e5\u770b\u76f8\u5e94\u7684\u53d8\u91cf\u3002<\/p>\n<p><strong>\u5982\u4f55\u6253\u5370\u7279\u5b9a\u53d8\u91cf\u7684\u503c\uff1f<\/strong><br \/>\u8981\u67e5\u770b\u7279\u5b9a\u53d8\u91cf\u7684\u503c\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u53d8\u91cf<code>my_variable<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>print(my_variable)<\/code>\u6765\u8f93\u51fa\u5176\u503c\u3002\u786e\u4fdd\u5728\u6253\u5370\u4e4b\u524d\uff0c\u8be5\u53d8\u91cf\u5df2\u7ecf\u88ab\u5b9a\u4e49\u5e76\u8d4b\u503c\u3002<\/p>\n<p><strong>\u5728Python\u8c03\u8bd5\u65f6\u5982\u4f55\u67e5\u770b\u53d8\u91cf\u7684\u503c\uff1f<\/strong><br \/>\u4f7f\u7528Python\u5185\u7f6e\u7684\u8c03\u8bd5\u5de5\u5177<code>pdb<\/code>\u53ef\u4ee5\u6709\u6548\u5730\u67e5\u770b\u53d8\u91cf\u7684\u503c\u3002\u5728\u8c03\u8bd5\u6a21\u5f0f\u4e0b\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>p<\/code>\u547d\u4ee4\u6765\u6253\u5370\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u8f93\u5165<code>p my_variable<\/code>\u5c06\u663e\u793a<code>my_variable<\/code>\u7684\u5f53\u524d\u503c\u3002\u8fd9\u5bf9\u4e8e\u9010\u6b65\u8c03\u8bd5\u4ee3\u7801\u975e\u5e38\u6709\u5e2e\u52a9\uff0c\u80fd\u591f\u8ba9\u60a8\u5b9e\u65f6\u76d1\u63a7\u53d8\u91cf\u7684\u72b6\u6001\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u67e5\u770bPython\u53d8\u91cf\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528print()\u51fd\u6570\u3001\u5229\u7528type()\u51fd\u6570\u3001\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\u3001\u4f7f\u7528dir()\u51fd\u6570 [&hellip;]","protected":false},"author":3,"featured_media":972277,"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\/972272"}],"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=972272"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/972272\/revisions"}],"predecessor-version":[{"id":972280,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/972272\/revisions\/972280"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/972277"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=972272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=972272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=972272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}