{"id":971779,"date":"2024-12-27T05:38:03","date_gmt":"2024-12-26T21:38:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/971779.html"},"modified":"2024-12-27T05:38:05","modified_gmt":"2024-12-26T21:38:05","slug":"python%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae%e5%8a%a8%e6%80%81%e5%91%bd%e5%90%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/971779.html","title":{"rendered":"python\u5982\u4f55\u8bbe\u7f6e\u52a8\u6001\u547d\u540d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194729\/9257ce8e-ab0f-4028-b2d1-86f0a0694d52.webp\" alt=\"python\u5982\u4f55\u8bbe\u7f6e\u52a8\u6001\u547d\u540d\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u52a8\u6001\u547d\u540d\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5b57\u5178\u3001\u5217\u8868\u3001<code>setattr()<\/code> \u51fd\u6570\u3001<code>globals()<\/code> \u548c <code>locals()<\/code> \u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u5b57\u5178\u548c\u5217\u8868\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u4e14\u76f4\u89c2\u7684\u65b9\u5f0f\u6765\u5b58\u50a8\u548c\u8bbf\u95ee\u52a8\u6001\u751f\u6210\u7684\u53d8\u91cf\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u5b57\u5178\u662f\u4e00\u79cd\u66f4\u4e3a\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u76f4\u89c2\uff0c\u800c\u4e14\u53ef\u4ee5\u907f\u514d\u547d\u540d\u51b2\u7a81\u7684\u95ee\u9898\u3002\u5b57\u5178\u53ef\u4ee5\u901a\u8fc7\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u5b58\u50a8\u6570\u636e\uff0c\u5e76\u4e14\u53ef\u4ee5\u52a8\u6001\u6dfb\u52a0\u3001\u4fee\u6539\u548c\u5220\u9664\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u5178\u7684\u4e00\u4e2a\u597d\u5904\u662f\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u7ba1\u7406\u548c\u8bbf\u95ee\u8fd9\u4e9b\u201c\u52a8\u6001\u547d\u540d\u201d\u7684\u53d8\u91cf\uff0c\u800c\u4e0d\u5fc5\u62c5\u5fc3\u547d\u540d\u51b2\u7a81\u6216\u4f5c\u7528\u57df\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u5927\u91cf\u52a8\u6001\u751f\u6210\u7684\u53d8\u91cf\u65f6\uff0c\u5b57\u5178\u53ef\u4ee5\u5e2e\u52a9\u4f60\u4fdd\u6301\u4ee3\u7801\u7684\u6e05\u6670\u548c\u6709\u5e8f\u3002\u901a\u8fc7\u5b57\u5178\u7684\u952e\u503c\u5bf9\uff0c\u4f60\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u5e76\u64cd\u4f5c\u7279\u5b9a\u7684\u53d8\u91cf\uff0c\u800c\u4e0d\u9700\u8981\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u67e5\u627e\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8Python\u4e2d\u51e0\u79cd\u5b9e\u73b0\u52a8\u6001\u547d\u540d\u7684\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5b57\u5178\u5b9e\u73b0\u52a8\u6001\u547d\u540d<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5b57\u5178\u662f\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u6765\u5b58\u50a8\u6570\u636e\u3002\u901a\u8fc7\u4f7f\u7528\u5b57\u5178\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u52a8\u6001\u547d\u540d\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u7684\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u6765\u5b58\u50a8\u548c\u8bbf\u95ee\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<\/p>\n<p>dynamic_variables = {}<\/p>\n<h2><strong>\u52a8\u6001\u6dfb\u52a0\u53d8\u91cf<\/strong><\/h2>\n<p>dynamic_variables[&#39;var1&#39;] = 10<\/p>\n<p>dynamic_variables[&#39;var2&#39;] = 20<\/p>\n<h2><strong>\u8bbf\u95ee\u53d8\u91cf<\/strong><\/h2>\n<p>print(dynamic_variables[&#39;var1&#39;])  # \u8f93\u51fa: 10<\/p>\n<p>print(dynamic_variables[&#39;var2&#39;])  # \u8f93\u51fa: 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u4e14\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u9700\u8981\u52a8\u6001\u547d\u540d\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>2. \u52a8\u6001\u751f\u6210\u952e<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u52a8\u6001\u751f\u6210\u952e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<\/p>\n<p>dynamic_variables = {}<\/p>\n<h2><strong>\u4f7f\u7528\u5faa\u73af\u52a8\u6001\u751f\u6210\u952e<\/strong><\/h2>\n<p>for i in range(5):<\/p>\n<p>    dynamic_key = f&quot;var{i}&quot;<\/p>\n<p>    dynamic_variables[dynamic_key] = i * 10<\/p>\n<h2><strong>\u8f93\u51fa\u6240\u6709\u53d8\u91cf<\/strong><\/h2>\n<p>for key, value in dynamic_variables.items():<\/p>\n<p>    print(f&quot;{key}: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5c24\u5176\u9002\u5408\u9700\u8981\u6279\u91cf\u751f\u6210\u53d8\u91cf\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u5b9e\u73b0\u52a8\u6001\u547d\u540d<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u4e5f\u662fPython\u4e2d\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u4e00\u7ec4\u6709\u5e8f\u7684\u6570\u636e\u3002\u867d\u7136\u5217\u8868\u6ca1\u6709\u5b57\u5178\u90a3\u6837\u7684\u952e\u503c\u5bf9\u7ed3\u6784\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u548c\u64cd\u4f5c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u5217\u8868\u7684\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u5217\u8868\u6765\u5b9e\u73b0\u52a8\u6001\u547d\u540d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868<\/p>\n<p>dynamic_variables = []<\/p>\n<h2><strong>\u52a8\u6001\u6dfb\u52a0\u53d8\u91cf<\/strong><\/h2>\n<p>for i in range(5):<\/p>\n<p>    dynamic_variables.append(i * 10)<\/p>\n<h2><strong>\u8bbf\u95ee\u53d8\u91cf<\/strong><\/h2>\n<p>for i in range(len(dynamic_variables)):<\/p>\n<p>    print(f&quot;var{i}: {dynamic_variables[i]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u6309\u5e8f\u8bbf\u95ee\u6216\u64cd\u4f5c\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>2. \u5217\u8868\u4e0e\u5b57\u5178\u7684\u7ed3\u5408<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u7684\u573a\u666f\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u5217\u8868\u548c\u5b57\u5178\u6765\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u52a8\u6001\u547d\u540d\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>dynamic_variables = []<\/p>\n<h2><strong>\u52a8\u6001\u6dfb\u52a0\u53d8\u91cf<\/strong><\/h2>\n<p>for i in range(5):<\/p>\n<p>    dynamic_variables.append({&#39;id&#39;: i, &#39;value&#39;: i * 10})<\/p>\n<h2><strong>\u8bbf\u95ee\u53d8\u91cf<\/strong><\/h2>\n<p>for item in dynamic_variables:<\/p>\n<p>    print(f&quot;id: {item[&#39;id&#39;]}, value: {item[&#39;value&#39;]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5b58\u50a8\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 <code>setattr()<\/code> \u51fd\u6570\u52a8\u6001\u8bbe\u7f6e\u5bf9\u8c61\u5c5e\u6027<\/h3>\n<\/p>\n<p><p><code>setattr()<\/code> \u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u52a8\u6001\u5730\u8bbe\u7f6e\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h4>1. <code>setattr()<\/code> \u7684\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>setattr()<\/code> \u4e3a\u4e00\u4e2a\u5bf9\u8c61\u52a8\u6001\u6dfb\u52a0\u5c5e\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class DynamicObject:<\/p>\n<p>    pass<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61<\/strong><\/h2>\n<p>obj = DynamicObject()<\/p>\n<h2><strong>\u52a8\u6001\u6dfb\u52a0\u5c5e\u6027<\/strong><\/h2>\n<p>setattr(obj, &#39;attr1&#39;, 10)<\/p>\n<p>setattr(obj, &#39;attr2&#39;, 20)<\/p>\n<h2><strong>\u8bbf\u95ee\u5c5e\u6027<\/strong><\/h2>\n<p>print(obj.attr1)  # \u8f93\u51fa: 10<\/p>\n<p>print(obj.attr2)  # \u8f93\u51fa: 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u4fee\u6539\u5bf9\u8c61\u5c5e\u6027\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>2. \u4e0e\u5faa\u73af\u7ed3\u5408\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u7ed3\u5408\u5faa\u73af\u4f7f\u7528 <code>setattr()<\/code> \u6765\u6279\u91cf\u8bbe\u7f6e\u5bf9\u8c61\u7684\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class DynamicObject:<\/p>\n<p>    pass<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61<\/strong><\/h2>\n<p>obj = DynamicObject()<\/p>\n<h2><strong>\u4f7f\u7528\u5faa\u73af\u52a8\u6001\u6dfb\u52a0\u5c5e\u6027<\/strong><\/h2>\n<p>for i in range(5):<\/p>\n<p>    dynamic_attr = f&quot;attr{i}&quot;<\/p>\n<p>    setattr(obj, dynamic_attr, i * 10)<\/p>\n<h2><strong>\u8f93\u51fa\u6240\u6709\u5c5e\u6027<\/strong><\/h2>\n<p>for i in range(5):<\/p>\n<p>    print(getattr(obj, f&quot;attr{i}&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u6279\u91cf\u751f\u6210\u5bf9\u8c61\u5c5e\u6027\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528 <code>globals()<\/code> \u548c <code>locals()<\/code> \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>globals()<\/code> \u548c <code>locals()<\/code> \u662fPython\u4e2d\u4e24\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u5206\u522b\u7528\u4e8e\u8bbf\u95ee\u5168\u5c40\u548c\u5c40\u90e8\u547d\u540d\u7a7a\u95f4\u3002<\/p>\n<\/p>\n<p><h4>1. <code>globals()<\/code> \u7684\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>globals()<\/code> \u6765\u52a8\u6001\u521b\u5efa\u5168\u5c40\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u52a8\u6001\u6dfb\u52a0\u5168\u5c40\u53d8\u91cf<\/p>\n<p>globals()[&#39;global_var1&#39;] = 10<\/p>\n<p>globals()[&#39;global_var2&#39;] = 20<\/p>\n<h2><strong>\u8bbf\u95ee\u5168\u5c40\u53d8\u91cf<\/strong><\/h2>\n<p>print(global_var1)  # \u8f93\u51fa: 10<\/p>\n<p>print(global_var2)  # \u8f93\u51fa: 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u6dfb\u52a0\u53d8\u91cf\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>2. <code>locals()<\/code> \u7684\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>locals()<\/code> \u4e0e <code>globals()<\/code> \u7c7b\u4f3c\uff0c\u53ea\u662f\u5b83\u7528\u4e8e\u8bbf\u95ee\u5c40\u90e8\u547d\u540d\u7a7a\u95f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def dynamic_locals():<\/p>\n<p>    # \u52a8\u6001\u6dfb\u52a0\u5c40\u90e8\u53d8\u91cf<\/p>\n<p>    locals()[&#39;local_var1&#39;] = 10<\/p>\n<p>    locals()[&#39;local_var2&#39;] = 20<\/p>\n<p>    # \u8bbf\u95ee\u5c40\u90e8\u53d8\u91cf<\/p>\n<p>    print(locals()[&#39;local_var1&#39;])  # \u8f93\u51fa: 10<\/p>\n<p>    print(locals()[&#39;local_var2&#39;])  # \u8f93\u51fa: 20<\/p>\n<p>dynamic_locals()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f7f\u7528 <code>locals()<\/code> \u52a8\u6001\u6dfb\u52a0\u5c40\u90e8\u53d8\u91cf\u65f6\uff0c\u53ef\u80fd\u4f1a\u53d7\u5230\u67d0\u4e9b\u9650\u5236\uff0c\u56e0\u4e3a <code>locals()<\/code> \u8fd4\u56de\u7684\u5b57\u5178\u5728\u67d0\u4e9b\u5b9e\u73b0\u4e2d\u53ef\u80fd\u662f\u53ea\u8bfb\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u52a8\u6001\u547d\u540d\u662f\u4e00\u9879\u975e\u5e38\u5b9e\u7528\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7075\u6d3b\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u53d8\u91cf\u3002\u901a\u8fc7\u4f7f\u7528\u5b57\u5178\u3001\u5217\u8868\u3001<code>setattr()<\/code> \u51fd\u6570\u3001<code>globals()<\/code> \u548c <code>locals()<\/code> \u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e0b\u5b9e\u73b0\u52a8\u6001\u547d\u540d\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u56e0\u6b64\u5728\u9009\u62e9\u65f6\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u6743\u8861\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u5173\u952e\u5728\u4e8e\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u7684\u5f00\u53d1\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u52a8\u6001\u547d\u540d\u7684\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u5b9e\u73b0\u52a8\u6001\u547d\u540d\u7684\u6548\u679c\u3002\u901a\u8fc7\u5c06\u53d8\u91cf\u540d\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u521b\u5efa\u548c\u8bbf\u95ee\u52a8\u6001\u547d\u540d\u7684\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>my_dict = {}<\/code>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178\uff0c\u7136\u540e\u901a\u8fc7<code>my_dict[&#39;dynamic_name&#39;] = value<\/code>\u6765\u6dfb\u52a0\u65b0\u7684\u52a8\u6001\u53d8\u91cf\u3002\u8fd9\u6837\uff0c\u53ef\u4ee5\u901a\u8fc7<code>my_dict[&#39;dynamic_name&#39;]<\/code>\u6765\u8bbf\u95ee\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<p><strong>\u52a8\u6001\u547d\u540d\u5bf9\u4ee3\u7801\u53ef\u8bfb\u6027\u6709\u4ec0\u4e48\u5f71\u54cd\uff1f<\/strong><br \/>\u52a8\u6001\u547d\u540d\u867d\u7136\u63d0\u4f9b\u4e86\u7075\u6d3b\u6027\uff0c\u4f46\u53ef\u80fd\u4f1a\u5f71\u54cd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u4f7f\u7528\u52a8\u6001\u547d\u540d\u65f6\uff0c\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u53ef\u80fd\u96be\u4ee5\u7406\u89e3\u53d8\u91cf\u7684\u610f\u4e49\u548c\u7528\u9014\u3002\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u5efa\u8bae\u4f7f\u7528\u660e\u786e\u7684\u547d\u540d\u89c4\u8303\uff0c\u6216\u8005\u5728\u9700\u8981\u52a8\u6001\u547d\u540d\u7684\u60c5\u51b5\u4e0b\uff0c\u9644\u52a0\u6ce8\u91ca\u4ee5\u8bf4\u660e\u5176\u76ee\u7684\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u52a8\u6001\u53d8\u91cf\u540d\u8fdb\u884c\u6570\u636e\u5b58\u50a8\uff1f<\/strong><br \/>\u53ef\u4ee5\u5229\u7528<code>globals()<\/code>\u6216<code>locals()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u52a8\u6001\u53d8\u91cf\u540d\uff0c\u8fd9\u4e9b\u51fd\u6570\u8fd4\u56de\u5f53\u524d\u5168\u5c40\u6216\u5c40\u90e8\u7684\u547d\u540d\u7a7a\u95f4\u3002\u901a\u8fc7\u5728\u8fd9\u4e9b\u547d\u540d\u7a7a\u95f4\u4e2d\u8bbe\u7f6e\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u5b9e\u73b0\u52a8\u6001\u5b58\u50a8\u3002\u4f8b\u5982\uff0c<code>globals()[&#39;dynamic_var&#39;] = value<\/code>\u5c06\u521b\u5efa\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf<code>dynamic_var<\/code>\uff0c\u5e76\u8d4b\u4e88\u5176\u503c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u65f6\u9700\u8c28\u614e\uff0c\u4ee5\u907f\u514d\u547d\u540d\u51b2\u7a81\u6216\u4e0d\u5fc5\u8981\u7684\u590d\u6742\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u52a8\u6001\u547d\u540d\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5b57\u5178\u3001\u5217\u8868\u3001setattr() \u51fd\u6570\u3001globals() \u548c local [&hellip;]","protected":false},"author":3,"featured_media":971783,"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\/971779"}],"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=971779"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/971779\/revisions"}],"predecessor-version":[{"id":971786,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/971779\/revisions\/971786"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/971783"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=971779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=971779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=971779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}