{"id":1180270,"date":"2025-01-15T18:34:46","date_gmt":"2025-01-15T10:34:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1180270.html"},"modified":"2025-01-15T18:34:49","modified_gmt":"2025-01-15T10:34:49","slug":"%e5%a6%82%e4%bd%95%e8%ae%a9python%e7%9a%84exec%e7%ba%bf%e6%80%a7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1180270.html","title":{"rendered":"\u5982\u4f55\u8ba9python\u7684exec\u7ebf\u6027"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25114447\/ed4f22c7-c544-4da6-a79e-4af62b6e1763.webp\" alt=\"\u5982\u4f55\u8ba9python\u7684exec\u7ebf\u6027\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<code>exec<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u52a8\u6001\u6267\u884cPython\u4ee3\u7801\u3002\u867d\u7136<code>exec<\/code>\u975e\u5e38\u5f3a\u5927\uff0c\u4f46\u4e5f\u6709\u5176\u7f3a\u70b9\uff0c\u5c24\u5176\u662f\u5728\u6027\u80fd\u548c\u5b89\u5168\u6027\u65b9\u9762\u3002\u5982\u679c\u8981\u8ba9Python\u7684<code>exec<\/code>\u51fd\u6570\u5728\u4f7f\u7528\u4e0a\u66f4\u52a0\u7ebf\u6027\uff0c\u53ef\u4ee5<strong>\u901a\u8fc7\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u91cd\u590d\u6267\u884c\u3001\u4f18\u5316\u4ee3\u7801\u903b\u8f91\u3001\u907f\u514d\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784<\/strong>\u7b49\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<strong>\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u91cd\u590d\u6267\u884c<\/strong>\u662f\u5b9e\u73b0\u7ebf\u6027\u6267\u884c\u7684\u4e00\u4e2a\u5173\u952e\u70b9\u3002<strong>\u4f8b\u5982\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u7f13\u5b58\u673a\u5236\u6216\u9884\u7f16\u8bd1\u4ee3\u7801\u6765\u51cf\u5c11\u91cd\u590d\u8ba1\u7b97\u7684\u5f00\u9500\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u91cd\u590d\u6267\u884c<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>exec<\/code>\u51fd\u6570\u65f6\uff0c\u907f\u514d\u91cd\u590d\u6267\u884c\u76f8\u540c\u7684\u4ee3\u7801\u7247\u6bb5\u662f\u63d0\u9ad8\u6267\u884c\u6548\u7387\u7684\u4e00\u4e2a\u91cd\u8981\u624b\u6bb5\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4ee3\u7801\u7f13\u5b58\u673a\u5236<\/h4>\n<\/p>\n<p><p>\u5728\u9700\u8981\u91cd\u590d\u6267\u884c\u76f8\u540c\u4ee3\u7801\u7684\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u7f13\u5b58\u673a\u5236\uff0c\u5c06\u5df2\u7ecf\u7f16\u8bd1\u7684\u4ee3\u7801\u7f13\u5b58\u8d77\u6765\uff0c\u907f\u514d\u6bcf\u6b21\u90fd\u91cd\u65b0\u7f16\u8bd1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import hashlib<\/p>\n<h2><strong>\u7f13\u5b58\u5b57\u5178<\/strong><\/h2>\n<p>code_cache = {}<\/p>\n<p>def exec_cached(code, context):<\/p>\n<p>    # \u751f\u6210\u4ee3\u7801\u7684\u54c8\u5e0c\u503c<\/p>\n<p>    code_hash = hashlib.md5(code.encode()).hexdigest()<\/p>\n<p>    # \u68c0\u67e5\u4ee3\u7801\u662f\u5426\u5df2\u7f13\u5b58<\/p>\n<p>    if code_hash in code_cache:<\/p>\n<p>        exec(code_cache[code_hash], context)<\/p>\n<p>    else:<\/p>\n<p>        # \u7f16\u8bd1\u4ee3\u7801<\/p>\n<p>        compiled_code = compile(code, &#39;&lt;string&gt;&#39;, &#39;exec&#39;)<\/p>\n<p>        code_cache[code_hash] = compiled_code<\/p>\n<p>        exec(compiled_code, context)<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>context = {}<\/p>\n<p>code_snippet = &quot;x = 10\\ny = x * 2\\nprint(y)&quot;<\/p>\n<p>exec_cached(code_snippet, context)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u907f\u514d\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784\u4f1a\u589e\u52a0\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\u548c\u590d\u6742\u5ea6\uff0c\u5c3d\u91cf\u7b80\u5316\u4ee3\u7801\u903b\u8f91\uff0c\u907f\u514d\u6df1\u5c42\u6b21\u7684\u5d4c\u5957\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7b80\u5316\u524d<\/p>\n<p>exec(&quot;&quot;&quot;<\/p>\n<p>if condition1:<\/p>\n<p>    if condition2:<\/p>\n<p>        if condition3:<\/p>\n<p>            # \u590d\u6742\u903b\u8f91<\/p>\n<p>            pass<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<h2><strong>\u7b80\u5316\u540e<\/strong><\/h2>\n<p>exec(&quot;&quot;&quot;<\/p>\n<p>if condition1 and condition2 and condition3:<\/p>\n<p>    # \u7b80\u5316\u540e\u7684\u903b\u8f91<\/p>\n<p>    pass<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f18\u5316\u4ee3\u7801\u903b\u8f91<\/h3>\n<\/p>\n<p><p>\u4f18\u5316\u4ee3\u7801\u903b\u8f91\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8<code>exec<\/code>\u51fd\u6570\u7684\u6267\u884c\u6548\u7387\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u548c\u91cd\u590d\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5728<code>exec<\/code>\u51fd\u6570\u4e2d\uff0c\u5c3d\u91cf\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\uff0c\u907f\u514d\u5168\u5c40\u53d8\u91cf\u7684\u9891\u7e41\u8bbf\u95ee\uff0c\u56e0\u4e3a\u5c40\u90e8\u53d8\u91cf\u7684\u8bbf\u95ee\u901f\u5ea6\u6bd4\u5168\u5c40\u53d8\u91cf\u5feb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5168\u5c40\u53d8\u91cf<\/p>\n<p>exec(&quot;&quot;&quot;<\/p>\n<p>global x<\/p>\n<p>x = 10<\/p>\n<p>y = x * 2<\/p>\n<p>print(y)<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf<\/strong><\/h2>\n<p>exec(&quot;&quot;&quot;<\/p>\n<p>x = 10<\/p>\n<p>y = x * 2<\/p>\n<p>print(y)<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u9884\u5148\u8ba1\u7b97\u5e38\u91cf\u503c<\/h4>\n<\/p>\n<p><p>\u5c06\u53ef\u4ee5\u9884\u5148\u8ba1\u7b97\u7684\u5e38\u91cf\u503c\u63d0\u524d\u8ba1\u7b97\u51fa\u6765\uff0c\u907f\u514d\u5728<code>exec<\/code>\u51fd\u6570\u4e2d\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u91cd\u590d\u8ba1\u7b97\u5e38\u91cf\u503c<\/p>\n<p>exec(&quot;&quot;&quot;<\/p>\n<p>result = (1000 * 1000) + (500 * 2)<\/p>\n<p>print(result)<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<h2><strong>\u9884\u5148\u8ba1\u7b97\u5e38\u91cf\u503c<\/strong><\/h2>\n<p>precomputed_value = (1000 * 1000) + (500 * 2)<\/p>\n<p>exec(&quot;&quot;&quot;<\/p>\n<p>result = precomputed_value<\/p>\n<p>print(result)<\/p>\n<p>&quot;&quot;&quot;, {&#39;precomputed_value&#39;: precomputed_value})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u52a8\u6001\u6267\u884c<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u9759\u6001\u4ee3\u7801\u66ff\u4ee3\u52a8\u6001\u6267\u884c\uff0c\u51cf\u5c11<code>exec<\/code>\u51fd\u6570\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6a21\u677f\u4ee3\u7801\u751f\u6210<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u6a21\u677f\u4ee3\u7801\u751f\u6210\u5668\uff0c\u5728\u9700\u8981\u52a8\u6001\u751f\u6210\u4ee3\u7801\u7684\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u6a21\u677f\u751f\u6210\u9759\u6001\u4ee3\u7801\uff0c\u907f\u514d\u4f7f\u7528<code>exec<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u52a8\u6001\u751f\u6210\u4ee3\u7801<\/p>\n<p>code_template = &quot;&quot;&quot;<\/p>\n<p>def generated_function(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>exec(code_template)<\/p>\n<p>result = generated_function(2, 3)<\/p>\n<p>print(result)<\/p>\n<h2><strong>\u4f7f\u7528\u6a21\u677f\u751f\u6210\u9759\u6001\u4ee3\u7801<\/strong><\/h2>\n<p>from string import Template<\/p>\n<p>template = Template(&quot;&quot;&quot;<\/p>\n<p>def $function_name(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<p>generated_code = template.substitute(function_name=&#39;generated_function&#39;)<\/p>\n<p>exec(generated_code)<\/p>\n<p>result = generated_function(2, 3)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u51fd\u6570\u6307\u9488<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u6307\u9488\u66ff\u4ee3<code>exec<\/code>\uff0c\u5b9e\u73b0\u52a8\u6001\u8c03\u7528\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528 exec \u52a8\u6001\u8c03\u7528\u51fd\u6570<\/p>\n<p>exec(&quot;&quot;&quot;<\/p>\n<p>def dynamic_function(x, y):<\/p>\n<p>    return x + y<\/p>\n<p>result = dynamic_function(2, 3)<\/p>\n<p>print(result)<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u51fd\u6570\u6307\u9488<\/strong><\/h2>\n<p>def dynamic_function(x, y):<\/p>\n<p>    return x + y<\/p>\n<p>function_ptr = dynamic_function<\/p>\n<p>result = function_ptr(2, 3)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u53ef\u4ee5\u51cf\u5c11\u9519\u8bef\u7684\u53d1\u751f\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u4f18\u5316\u548c\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6dfb\u52a0\u6ce8\u91ca\u548c\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u8be6\u7ec6\u7684\u6ce8\u91ca\u548c\u6587\u6863\uff0c\u6709\u52a9\u4e8e\u7406\u89e3\u4ee3\u7801\u903b\u8f91\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u4f18\u5316\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u6ce8\u91ca<\/p>\n<p>exec(&quot;&quot;&quot;<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u548c<\/strong><\/h2>\n<p>def add_numbers(a, b):<\/p>\n<p>    return a + b<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\uff0c\u8ba1\u7b97\u7ed3\u679c<\/strong><\/h2>\n<p>result = add_numbers(2, 3)<\/p>\n<p>print(result)<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u6a21\u5757\u5316\u8bbe\u8ba1<\/h4>\n<\/p>\n<p><p>\u5c06\u4ee3\u7801\u6a21\u5757\u5316\uff0c\u5206\u79bb\u4e0d\u540c\u529f\u80fd\u7684\u4ee3\u7801\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6a21\u5757\u5316\u8bbe\u8ba1<\/p>\n<p>def add_numbers(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    result = add_numbers(2, 3)<\/p>\n<p>    print(result)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6ce8\u610f\u5b89\u5168\u6027<\/h3>\n<\/p>\n<p><p><code>exec<\/code>\u51fd\u6570\u5177\u6709\u5f88\u9ad8\u7684\u6743\u9650\uff0c\u80fd\u591f\u6267\u884c\u4efb\u610f\u7684Python\u4ee3\u7801\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u65f6\u8981\u7279\u522b\u6ce8\u610f\u5b89\u5168\u6027\uff0c\u907f\u514d\u6267\u884c\u6076\u610f\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u9650\u5236\u6267\u884c\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u9650\u5236<code>exec<\/code>\u51fd\u6570\u7684\u6267\u884c\u73af\u5883\uff0c\u53ef\u4ee5\u907f\u514d\u6076\u610f\u4ee3\u7801\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9650\u5236\u6267\u884c\u73af\u5883<\/p>\n<p><a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_context = {&#39;__builtins__&#39;: None}<\/p>\n<p>exec(&quot;&quot;&quot;<\/p>\n<p>result = 2 + 3<\/p>\n<p>print(result)<\/p>\n<p>&quot;&quot;&quot;, safe_context)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u9a8c\u8bc1\u8f93\u5165\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u5728\u6267\u884c\u4ee3\u7801\u524d\uff0c\u5bf9\u8f93\u5165\u4ee3\u7801\u8fdb\u884c\u9a8c\u8bc1\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9a8c\u8bc1\u8f93\u5165\u4ee3\u7801<\/p>\n<p>def is_safe_code(code):<\/p>\n<p>    # \u7b80\u5355\u7684\u9a8c\u8bc1\u793a\u4f8b\uff0c\u53ea\u5141\u8bb8\u52a0\u6cd5\u8fd0\u7b97<\/p>\n<p>    allowed_chars = set(&#39;0123456789+ \\n&#39;)<\/p>\n<p>    return all(char in allowed_chars for char in code)<\/p>\n<p>code_snippet = &quot;2 + 3&quot;<\/p>\n<p>if is_safe_code(code_snippet):<\/p>\n<p>    exec(code_snippet)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Unsafe code detected!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728<code>exec<\/code>\u51fd\u6570\u4e2d\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u7684\u6267\u884c\u6548\u7387\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u66ff\u4ee3\u5217\u8868\uff08list\uff09\u8fdb\u884c\u67e5\u627e\u64cd\u4f5c\uff0c\u53ef\u4ee5\u63d0\u9ad8\u67e5\u627e\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5217\u8868<\/p>\n<p>exec(&quot;&quot;&quot;<\/p>\n<p>data = [1, 2, 3, 4, 5]<\/p>\n<p>if 3 in data:<\/p>\n<p>    print(&quot;Found&quot;)<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u96c6\u5408<\/strong><\/h2>\n<p>exec(&quot;&quot;&quot;<\/p>\n<p>data = {1, 2, 3, 4, 5}<\/p>\n<p>if 3 in data:<\/p>\n<p>    print(&quot;Found&quot;)<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f18\u5316\u7b97\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f18\u5316\u7b97\u6cd5\uff0c\u53ef\u4ee5\u51cf\u5c11\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u66ff\u4ee3\u7ebf\u6027\u67e5\u627e\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u67e5\u627e\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7ebf\u6027\u67e5\u627e<\/p>\n<p>exec(&quot;&quot;&quot;<\/p>\n<p>def linear_search(data, target):<\/p>\n<p>    for item in data:<\/p>\n<p>        if item == target:<\/p>\n<p>            return True<\/p>\n<p>    return False<\/p>\n<p>data = [1, 2, 3, 4, 5]<\/p>\n<p>result = linear_search(data, 3)<\/p>\n<p>print(result)<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<h2><strong>\u4e8c\u5206\u67e5\u627e<\/strong><\/h2>\n<p>exec(&quot;&quot;&quot;<\/p>\n<p>def binary_search(data, target):<\/p>\n<p>    left, right = 0, len(data) - 1<\/p>\n<p>    while left &lt;= right:<\/p>\n<p>        mid = (left + right) \/\/ 2<\/p>\n<p>        if data[mid] == target:<\/p>\n<p>            return True<\/p>\n<p>        elif data[mid] &lt; target:<\/p>\n<p>            left = mid + 1<\/p>\n<p>        else:<\/p>\n<p>            right = mid - 1<\/p>\n<p>    return False<\/p>\n<p>data = [1, 2, 3, 4, 5]<\/p>\n<p>result = binary_search(data, 3)<\/p>\n<p>print(result)<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<p>### \u4e03\u3001\u4f7f\u7528\u5e76\u884c\u548c\u5f02\u6b65\u7f16\u7a0b<\/p>\n<p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u5e76\u884c\u548c\u5f02\u6b65\u7f16\u7a0b\uff0c\u63d0\u9ad8`exec`\u51fd\u6570\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<p>#### 1\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b<\/p>\n<p>\u901a\u8fc7\u4f7f\u7528\u591a\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u5e76\u884c\u6267\u884c\u591a\u4e2a\u4ee3\u7801\u7247\u6bb5\uff0c\u63d0\u9ad8\u6267\u884c\u6548\u7387\u3002<\/p>\n<p>```python<\/p>\n<p>import threading<\/p>\n<p>def exec_in_thread(code, context):<\/p>\n<p>    exec(code, context)<\/p>\n<p>context1 = {&#39;result&#39;: None}<\/p>\n<p>context2 = {&#39;result&#39;: None}<\/p>\n<p>code1 = &quot;result = sum(range(1000000))&quot;<\/p>\n<p>code2 = &quot;result = sum(range(1000000, 2000000))&quot;<\/p>\n<p>thread1 = threading.Thread(target=exec_in_thread, args=(code1, context1))<\/p>\n<p>thread2 = threading.Thread(target=exec_in_thread, args=(code2, context2))<\/p>\n<p>thread1.start()<\/p>\n<p>thread2.start()<\/p>\n<p>thread1.join()<\/p>\n<p>thread2.join()<\/p>\n<p>print(context1[&#39;result&#39;])<\/p>\n<p>print(context2[&#39;result&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b\uff0c\u53ef\u4ee5\u5728\u7b49\u5f85I\/O\u64cd\u4f5c\u65f6\u6267\u884c\u5176\u4ed6\u4ee3\u7801\uff0c\u63d0\u9ad8\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def exec_async(code, context):<\/p>\n<p>    exec(code, context)<\/p>\n<p>context1 = {&#39;result&#39;: None}<\/p>\n<p>context2 = {&#39;result&#39;: None}<\/p>\n<p>code1 = &quot;result = sum(range(1000000))&quot;<\/p>\n<p>code2 = &quot;result = sum(range(1000000, 2000000))&quot;<\/p>\n<p>async def main():<\/p>\n<p>    await asyncio.gather(<\/p>\n<p>        exec_async(code1, context1),<\/p>\n<p>        exec_async(code2, context2)<\/p>\n<p>    )<\/p>\n<p>asyncio.run(main())<\/p>\n<p>print(context1[&#39;result&#39;])<\/p>\n<p>print(context2[&#39;result&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u907f\u514d\u8fc7\u5ea6\u4f7f\u7528<code>exec<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u867d\u7136<code>exec<\/code>\u51fd\u6570\u975e\u5e38\u5f3a\u5927\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u65b9\u5f0f\u66ff\u4ee3<code>exec<\/code>\uff0c\u51cf\u5c11\u5176\u4f7f\u7528\u9891\u7387\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u5b89\u5168\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u9700\u8981\u52a8\u6001\u751f\u6210\u4ee3\u7801\u7684\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u6765\u66ff\u4ee3<code>exec<\/code>\uff0c\u907f\u514d\u6267\u884c\u4efb\u610f\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u914d\u7f6e\u6587\u4ef6<\/strong><\/h2>\n<p>config = {<\/p>\n<p>    &quot;operation&quot;: &quot;add&quot;,<\/p>\n<p>    &quot;operands&quot;: [2, 3]<\/p>\n<p>}<\/p>\n<h2><strong>\u6267\u884c\u64cd\u4f5c<\/strong><\/h2>\n<p>if config[&#39;operation&#39;] == &#39;add&#39;:<\/p>\n<p>    result = sum(config[&#39;operands&#39;])<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u88c5\u9970\u5668<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u88c5\u9970\u5668\u6765\u52a8\u6001\u4fee\u6539\u51fd\u6570\u884c\u4e3a\uff0c\u66ff\u4ee3<code>exec<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528 exec \u52a8\u6001\u4fee\u6539\u51fd\u6570\u884c\u4e3a<\/p>\n<p>exec(&quot;&quot;&quot;<\/p>\n<p>def dynamic_function(x):<\/p>\n<p>    return x * 2<\/p>\n<p>result = dynamic_function(3)<\/p>\n<p>print(result)<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u88c5\u9970\u5668<\/strong><\/h2>\n<p>def multiply_by_two(func):<\/p>\n<p>    def wrapper(x):<\/p>\n<p>        return func(x) * 2<\/p>\n<p>    return wrapper<\/p>\n<p>@multiply_by_two<\/p>\n<p>def dynamic_function(x):<\/p>\n<p>    return x<\/p>\n<p>result = dynamic_function(3)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u66f4\u9ad8\u6548\u7684Python\u89e3\u91ca\u5668<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u66f4\u9ad8\u6548\u7684Python\u89e3\u91ca\u5668\uff08\u5982PyPy\uff09\uff0c\u63d0\u9ad8<code>exec<\/code>\u51fd\u6570\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u548c\u4f7f\u7528PyPy<\/h4>\n<\/p>\n<p><p>PyPy\u662f\u4e00\u4e2a\u9ad8\u6548\u7684Python\u89e3\u91ca\u5668\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># \u5b89\u88c5 PyPy<\/p>\n<p>sudo apt-get install pypy<\/p>\n<h2><strong>\u4f7f\u7528 PyPy \u8fd0\u884c Python \u4ee3\u7801<\/strong><\/h2>\n<p>pypy script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6bd4\u8f83\u6027\u80fd<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u6bd4\u8f83CPython\u548cPyPy\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u53d1\u73b0PyPy\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u5177\u6709\u663e\u8457\u7684\u6027\u80fd\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u4f7f\u7528 CPython<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>exec(&quot;result = sum(range(1000000))&quot;)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;CPython execution time:&quot;, end_time - start_time)<\/p>\n<h2><strong>\u4f7f\u7528 PyPy<\/strong><\/h2>\n<h2><strong>\u8fd0\u884c\u76f8\u540c\u7684\u4ee3\u7801\uff0c\u6bd4\u8f83\u6267\u884c\u65f6\u95f4<\/strong><\/h2>\n<h2><strong>pypy script.py<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u9ad8Python <code>exec<\/code>\u51fd\u6570\u7684\u6267\u884c\u6548\u7387\uff0c\u5b9e\u73b0\u66f4\u52a0\u7ebf\u6027\u7684\u6267\u884c\u3002<strong>\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u91cd\u590d\u6267\u884c\u3001\u4f18\u5316\u4ee3\u7801\u903b\u8f91\u3001\u907f\u514d\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784\u3001\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3001\u6ce8\u610f\u5b89\u5168\u6027\u3001\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u3001\u4f7f\u7528\u5e76\u884c\u548c\u5f02\u6b65\u7f16\u7a0b\u3001\u907f\u514d\u8fc7\u5ea6\u4f7f\u7528<code>exec<\/code>\u51fd\u6570\u3001\u4f7f\u7528\u66f4\u9ad8\u6548\u7684Python\u89e3\u91ca\u5668<\/strong>\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u4f7f\u7528<code>exec<\/code>\u51fd\u6570\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u6267\u884c\u6548\u7387\u548c\u5b89\u5168\u6027\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5e94\u8be5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u63d0\u9ad8Python\u4e2dexec\u7684\u6027\u80fd\uff1f<\/strong><br \/>\u5728Python\u4e2d\u4f7f\u7528exec\u51fd\u6570\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u53d7\u5230\u5f71\u54cd\u3002\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7f16\u8bd1\u4ee3\u7801\u7684\u65b9\u5f0f\uff0c\u4f8b\u5982\u4f7f\u7528compile\u51fd\u6570\u5c06\u4ee3\u7801\u7f16\u8bd1\u6210\u5b57\u8282\u7801\uff0c\u7136\u540e\u4f7f\u7528exec\u6765\u6267\u884c\u7f16\u8bd1\u540e\u7684\u4ee3\u7801\u3002\u6b64\u5916\uff0c\u4f18\u5316\u4ee3\u7801\u903b\u8f91\uff0c\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u548c\u91cd\u590d\u6267\u884c\u4e5f\u662f\u63d0\u5347\u6027\u80fd\u7684\u6709\u6548\u65b9\u6cd5\u3002<\/p>\n<p><strong>exec\u51fd\u6570\u5728\u4ec0\u4e48\u573a\u666f\u4e0b\u4f7f\u7528\u6548\u679c\u6700\u4f73\uff1f<\/strong><br \/>exec\u51fd\u6570\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u6267\u884c\u4ee3\u7801\u7684\u573a\u666f\uff0c\u4f8b\u5982\u5728\u89e3\u91ca\u5668\u73af\u5883\u4e2d\u6267\u884c\u7528\u6237\u8f93\u5165\u7684\u4ee3\u7801\u3001\u52a8\u6001\u751f\u6210\u811a\u672c\u6216\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b\u6267\u884c\u4e0d\u540c\u7684\u4ee3\u7801\u5757\u3002\u5c3d\u7ba1exec\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u5728\u9700\u8981\u9ad8\u6027\u80fd\u7684\u5e94\u7528\u4e2d\uff0c\u5efa\u8bae\u8c28\u614e\u4f7f\u7528\uff0c\u5e76\u8003\u8651\u5176\u4ed6\u66ff\u4ee3\u65b9\u6cd5\uff0c\u5982\u51fd\u6570\u6216\u7c7b\u7684\u5b9a\u4e49\u3002<\/p>\n<p><strong>exec\u5bf9\u5b89\u5168\u6027\u7684\u5f71\u54cd\u6709\u591a\u5927\uff1f<\/strong><br \/>\u4f7f\u7528exec\u65f6\uff0c\u4ee3\u7801\u7684\u6267\u884c\u73af\u5883\u662f\u52a8\u6001\u751f\u6210\u7684\uff0c\u56e0\u6b64\u5b58\u5728\u5b89\u5168\u98ce\u9669\u3002\u6267\u884c\u6765\u81ea\u4e0d\u53ef\u4fe1\u6e90\u7684\u4ee3\u7801\u53ef\u80fd\u5bfc\u81f4\u6076\u610f\u64cd\u4f5c\u6216\u6570\u636e\u6cc4\u9732\u3002\u4e3a\u6b64\uff0c\u5efa\u8bae\u5728\u4f7f\u7528exec\u65f6\u4e25\u683c\u63a7\u5236\u8f93\u5165\u6765\u6e90\uff0c\u5fc5\u8981\u65f6\u5bf9\u8f93\u5165\u8fdb\u884c\u6821\u9a8c\u548c\u6e05\u7406\uff0c\u786e\u4fdd\u4ee3\u7801\u6267\u884c\u7684\u5b89\u5168\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0cexec\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u52a8\u6001\u6267\u884cPython\u4ee3\u7801\u3002\u867d\u7136exec\u975e\u5e38\u5f3a\u5927\uff0c\u4f46\u4e5f\u6709\u5176\u7f3a\u70b9\uff0c\u5c24\u5176\u662f\u5728\u6027 [&hellip;]","protected":false},"author":3,"featured_media":1180276,"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\/1180270"}],"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=1180270"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1180270\/revisions"}],"predecessor-version":[{"id":1180279,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1180270\/revisions\/1180279"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1180276"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1180270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1180270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1180270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}