{"id":1000991,"date":"2024-12-27T09:55:51","date_gmt":"2024-12-27T01:55:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1000991.html"},"modified":"2024-12-27T09:56:01","modified_gmt":"2024-12-27T01:56:01","slug":"python%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e6%89%80%e6%9c%89%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1000991.html","title":{"rendered":"python\u5982\u4f55\u5220\u9664\u6240\u6709\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075349\/c8c5bfd0-582d-4433-9728-b8091e75b090.webp\" alt=\"python\u5982\u4f55\u5220\u9664\u6240\u6709\u53d8\u91cf\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5220\u9664\u6240\u6709\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u91cd\u542f\u89e3\u91ca\u5668\u3001\u4f7f\u7528<code>globals()<\/code>\u548c<code>del<\/code>\u547d\u4ee4\u3002\u91cd\u542f\u89e3\u91ca\u5668\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u4f46\u53ef\u80fd\u4e0d\u65b9\u4fbf\uff0c\u4f7f\u7528<code>globals()<\/code>\u548c<code>del<\/code>\u547d\u4ee4\u53ef\u4ee5\u624b\u52a8\u5220\u9664\u53d8\u91cf\uff0c\u786e\u4fdd\u6ca1\u6709\u8bef\u5220\u91cd\u8981\u7684\u7cfb\u7edf\u53d8\u91cf\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u91cd\u542f\u89e3\u91ca\u5668\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5728\u5927\u591a\u6570\u5f00\u53d1\u73af\u5883\u4e2d\uff0c\u91cd\u542fPython\u89e3\u91ca\u5668\u4f1a\u81ea\u52a8\u6e05\u9664\u6240\u6709\u5185\u5b58\u4e2d\u7684\u53d8\u91cf\u3002\u5c3d\u7ba1\u8fd9\u662f\u4e00\u79cd\u975e\u5e38\u6709\u6548\u7684\u65b9\u5f0f\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u91cd\u65b0\u542f\u52a8\u89e3\u91ca\u5668\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e22\u5931\u5c1a\u672a\u4fdd\u5b58\u7684\u5de5\u4f5c\u3002\u56e0\u6b64\uff0c\u4e86\u89e3\u5982\u4f55\u624b\u52a8\u5220\u9664\u53d8\u91cf\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>globals()<\/code>\u548c<code>del<\/code>\u547d\u4ee4\u5219\u662f\u4e00\u79cd\u66f4\u4e3a\u7ec6\u81f4\u7684\u65b9\u6cd5\u3002<code>globals()<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\uff0c\u8be5\u5b57\u5178\u5305\u542b\u5f53\u524d\u5168\u5c40\u7b26\u53f7\u8868\u4e2d\u7684\u6240\u6709\u53d8\u91cf\u3002\u901a\u8fc7\u8fed\u4ee3\u8fd9\u4e2a\u5b57\u5178\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u6027\u5730\u5220\u9664\u81ea\u5df1\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u800c\u4e0d\u5f71\u54cdPython\u5185\u7f6e\u7684\u5168\u5c40\u53d8\u91cf\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u4fdd\u6301\u89e3\u91ca\u5668\u8fd0\u884c\u7684\u540c\u65f6\u6e05\u7406\u5de5\u4f5c\u7a7a\u95f4\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u5220\u9664\u6240\u6709\u53d8\u91cf\uff0c\u5305\u62ec\u5176\u4f18\u70b9\u548c\u5c40\u9650\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u91cd\u542f\u89e3\u91ca\u5668<\/strong><\/h2>\n<p><p>\u91cd\u542fPython\u89e3\u91ca\u5668\u662f\u5220\u9664\u6240\u6709\u53d8\u91cf\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u3002\u5728\u5927\u591a\u6570\u5f00\u53d1\u73af\u5883\u4e2d\uff0c\u5982Jupyter Notebook\u3001IPython\u3001PyCharm\u7b49\uff0c\u91cd\u65b0\u542f\u52a8\u89e3\u91ca\u5668\u53ef\u4ee5\u6e05\u9664\u6240\u6709\u53d8\u91cf\u548c\u6267\u884c\u7684\u4ee3\u7801\uff0c\u4f7f\u5f97\u73af\u5883\u91cd\u7f6e\u4e3a\u521d\u59cb\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h2>\u4f18\u70b9<\/h2>\n<\/p>\n<ol>\n<li><strong>\u7b80\u5355\u76f4\u63a5<\/strong>\uff1a\u91cd\u542f\u89e3\u91ca\u5668\u4e0d\u9700\u8981\u4efb\u4f55\u4ee3\u7801\u64cd\u4f5c\uff0c\u53ea\u9700\u4e00\u4e2a\u6309\u94ae\u6216\u547d\u4ee4\u3002<\/li>\n<li><strong>\u5f7b\u5e95\u6e05\u9664<\/strong>\uff1a\u80fd\u591f\u786e\u4fdd\u6240\u6709\u7684\u5185\u5b58\u548c\u53d8\u91cf\u90fd\u88ab\u6e05\u9664\uff0c\u4e0d\u4f1a\u6709\u9057\u6f0f\u3002<\/li>\n<\/ol>\n<p><h2>\u5c40\u9650\u6027<\/h2>\n<\/p>\n<ol>\n<li><strong>\u4e22\u5931\u672a\u4fdd\u5b58\u7684\u5de5\u4f5c<\/strong>\uff1a\u91cd\u542f\u89e3\u91ca\u5668\u5c06\u4f1a\u4e22\u5931\u6240\u6709\u672a\u4fdd\u5b58\u7684\u6570\u636e\u548c\u4ee3\u7801\uff0c\u9700\u8981\u786e\u4fdd\u6240\u6709\u91cd\u8981\u4fe1\u606f\u5df2\u4fdd\u5b58\u3002<\/li>\n<li><strong>\u4e0d\u9002\u5408\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u7a0b\u5e8f<\/strong>\uff1a\u5982\u679c\u5728\u8fd0\u884c\u4e00\u4e2a\u957f\u65f6\u95f4\u7a0b\u5e8f\u4e2d\u91cd\u542f\u89e3\u91ca\u5668\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u4e2d\u65ad\uff0c\u9700\u8981\u91cd\u65b0\u5f00\u59cb\u3002<\/li>\n<\/ol>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528<code>globals()<\/code>\u548c<code>del<\/code>\u547d\u4ee4<\/strong><\/h2>\n<p><p>\u5728\u4e0d\u91cd\u542f\u89e3\u91ca\u5668\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7<code>globals()<\/code>\u51fd\u6570\u7ed3\u5408<code>del<\/code>\u547d\u4ee4\u6765\u5220\u9664\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528<code>globals()<\/code><\/h2>\n<\/p>\n<p><p><code>globals()<\/code>\u51fd\u6570\u8fd4\u56de\u5f53\u524d\u6a21\u5757\u4e2d\u5b9a\u4e49\u7684\u5168\u5c40\u53d8\u91cf\u7684\u5b57\u5178\u3002\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u8fd9\u4e2a\u5b57\u5178\u6765\u5220\u9664\u6240\u6709\u7528\u6237\u5b9a\u4e49\u7684\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for name in list(globals().keys()):<\/p>\n<p>    if not name.startswith(&#39;_&#39;):<\/p>\n<p>        del globals()[name]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4f18\u70b9<\/h2>\n<\/p>\n<ol>\n<li><strong>\u7075\u6d3b\u63a7\u5236<\/strong>\uff1a\u53ef\u4ee5\u6709\u9009\u62e9\u5730\u5220\u9664\u53d8\u91cf\uff0c\u4e0d\u5fc5\u5168\u90e8\u6e05\u9664\u3002<\/li>\n<li><strong>\u4fdd\u6301\u73af\u5883\u8fd0\u884c<\/strong>\uff1a\u4e0d\u9700\u8981\u91cd\u542f\u89e3\u91ca\u5668\uff0c\u53ef\u4ee5\u7ee7\u7eed\u5f53\u524d\u7684\u5de5\u4f5c\u3002<\/li>\n<\/ol>\n<p><h2>\u5c40\u9650\u6027<\/h2>\n<\/p>\n<ol>\n<li><strong>\u53ef\u80fd\u8bef\u5220\u7cfb\u7edf\u53d8\u91cf<\/strong>\uff1a\u9700\u8981\u5c0f\u5fc3\u64cd\u4f5c\uff0c\u4ee5\u514d\u8bef\u5220Python\u5185\u7f6e\u7684\u5168\u5c40\u53d8\u91cf\u3002<\/li>\n<li><strong>\u590d\u6742\u5ea6\u8f83\u9ad8<\/strong>\uff1a\u5bf9\u4e8e\u521d\u5b66\u8005\u53ef\u80fd\u4e0d\u592a\u76f4\u89c2\uff0c\u9700\u8981\u4e00\u5b9a\u7684Python\u77e5\u8bc6\u3002<\/li>\n<\/ol>\n<h2><strong>\u4e09\u3001\u5176\u4ed6\u65b9\u6cd5<\/strong><\/h2>\n<p><h2>\u4f7f\u7528<code>locals()<\/code><\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8e\u51fd\u6570\u6216\u7c7b\u65b9\u6cd5\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528<code>locals()<\/code>\u51fd\u6570\u6765\u5220\u9664\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def clear_local_vars():<\/p>\n<p>    local_vars = list(locals().keys())<\/p>\n<p>    for var in local_vars:<\/p>\n<p>        del locals()[var]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5c40\u90e8\u53d8\u91cf\u7684\u6e05\u7406\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>locals()<\/code>\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5b57\u5178\u7684\u526f\u672c\uff0c\u76f4\u63a5\u5220\u9664\u5e76\u4e0d\u4f1a\u5f71\u54cd\u539f\u6765\u7684\u5c40\u90e8\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528<code>gc<\/code>\u6a21\u5757<\/h2>\n<\/p>\n<p><p><code>gc<\/code>\u6a21\u5757\u7528\u4e8e\u63a7\u5236Python\u5783\u573e\u56de\u6536\u5668\u3002\u901a\u8fc7\u8c03\u7528<code>gc.collect()<\/code>\u53ef\u4ee5\u5f3a\u5236\u8fdb\u884c\u4e00\u6b21\u5783\u573e\u56de\u6536\uff0c\u4f46\u8fd9\u5e76\u4e0d\u80fd\u5220\u9664\u53d8\u91cf\uff0c\u800c\u662f\u6e05\u7406\u672a\u5f15\u7528\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import gc<\/p>\n<p>gc.collect()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4f7f\u7528<code>dir()<\/code>\u548c<code>setattr()<\/code><\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>dir()<\/code>\u51fd\u6570\u83b7\u53d6\u5f53\u524d\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\u5217\u8868\uff0c\u5e76\u4f7f\u7528<code>setattr()<\/code>\u8fdb\u884c\u5220\u9664\u64cd\u4f5c\u3002\u8fd9\u79cd\u65b9\u6cd5\u9700\u8981\u8fdb\u884c\u66f4\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u901a\u5e38\u4e0d\u5efa\u8bae\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for var in dir():<\/p>\n<p>    if not var.startswith(&#39;_&#39;):<\/p>\n<p>        del globals()[var]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5220\u9664\u6240\u6709\u53d8\u91cf\u7684\u65b9\u6cd5\u6709\u591a\u79cd\u9009\u62e9\uff0c<strong>\u91cd\u542f\u89e3\u91ca\u5668\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5<\/strong>\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002\u5bf9\u4e8e\u9700\u8981\u4fdd\u6301\u73af\u5883\u8fd0\u884c\u7684\u60c5\u51b5\uff0c\u4f7f\u7528<code>globals()<\/code>\u548c<code>del<\/code>\u547d\u4ee4\u5219\u66f4\u52a0\u7075\u6d3b\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u6765\u51b3\u5b9a\uff0c\u786e\u4fdd\u4e0d\u4f1a\u8bef\u5220\u91cd\u8981\u7684\u53d8\u91cf\u548c\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4e00\u6b21\u6027\u6e05\u9664\u6240\u6709\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>globals()<\/code>\u51fd\u6570\u7ed3\u5408<code>del<\/code>\u8bed\u53e5\u6765\u5220\u9664\u6240\u6709\u5168\u5c40\u53d8\u91cf\u3002\u5177\u4f53\u64cd\u4f5c\u662f\u83b7\u53d6\u5f53\u524d\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u5b57\u5178\uff0c\u904d\u5386\u5176\u4e2d\u7684\u53d8\u91cf\u540d\u79f0\u5e76\u9010\u4e00\u5220\u9664\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u6837\u7684\u64cd\u4f5c\u4f1a\u6e05\u7a7a\u6240\u6709\u53d8\u91cf\uff0c\u5305\u62ec\u4f60\u53ef\u80fd\u9700\u8981\u7684\u7cfb\u7edf\u53d8\u91cf\uff0c\u6240\u4ee5\u4f7f\u7528\u65f6\u9700\u8c28\u614e\u3002<\/p>\n<p><strong>\u5220\u9664\u5c40\u90e8\u53d8\u91cf\u4e0e\u5168\u5c40\u53d8\u91cf\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><br \/>\u5c40\u90e8\u53d8\u91cf\u662f\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\uff0c\u800c\u5168\u5c40\u53d8\u91cf\u662f\u5728\u6574\u4e2a\u811a\u672c\u4e2d\u90fd\u80fd\u8bbf\u95ee\u7684\u53d8\u91cf\u3002\u8981\u5220\u9664\u5c40\u90e8\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\uff0c\u800c\u5220\u9664\u5168\u5c40\u53d8\u91cf\u5219\u9700\u8981\u4f7f\u7528<code>globals()<\/code>\u51fd\u6570\u3002\u5220\u9664\u5c40\u90e8\u53d8\u91cf\u65f6\u9700\u8981\u786e\u4fdd\u4e0d\u4f1a\u5f71\u54cd\u5230\u51fd\u6570\u7684\u6b63\u5e38\u6267\u884c\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u624b\u52a8\u5220\u9664\u53d8\u91cf\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6216\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u7ba1\u7406\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u3002\u901a\u8fc7\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u5757\u7ed3\u675f\u65f6\u81ea\u52a8\u6e05\u7406\u53d8\u91cf\uff0c\u907f\u514d\u624b\u52a8\u5220\u9664\u7684\u9ebb\u70e6\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u5bf9\u8c61\u7684\u5c5e\u6027\u6765\u4ee3\u66ff\u5168\u5c40\u53d8\u91cf\uff0c\u8fd9\u6837\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u7684\u9500\u6bc1\u6765\u81ea\u52a8\u6e05\u7406\u76f8\u5173\u53d8\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5220\u9664\u6240\u6709\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u91cd\u542f\u89e3\u91ca\u5668\u3001\u4f7f\u7528globals()\u548cdel\u547d\u4ee4\u3002\u91cd\u542f\u89e3\u91ca\u5668\u662f\u6700 [&hellip;]","protected":false},"author":3,"featured_media":1001013,"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\/1000991"}],"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=1000991"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1000991\/revisions"}],"predecessor-version":[{"id":1001015,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1000991\/revisions\/1001015"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1001013"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1000991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1000991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1000991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}