{"id":1008971,"date":"2024-12-27T11:07:31","date_gmt":"2024-12-27T03:07:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1008971.html"},"modified":"2024-12-27T11:07:35","modified_gmt":"2024-12-27T03:07:35","slug":"python%e5%a6%82%e4%bd%95%e8%bf%90%e8%a1%8c%e6%8c%87%e5%ae%9a%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1008971.html","title":{"rendered":"python\u5982\u4f55\u8fd0\u884c\u6307\u5b9a\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084102\/a52b68af-5444-4b3f-a12e-14b7296ec43b.webp\" alt=\"python\u5982\u4f55\u8fd0\u884c\u6307\u5b9a\u51fd\u6570\" \/><\/p>\n<p><p> <strong>Python\u8fd0\u884c\u6307\u5b9a\u51fd\u6570\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u51fd\u6570\u540d\u76f4\u63a5\u8c03\u7528\u3001\u901a\u8fc7\u5b57\u7b26\u4e32\u540d\u79f0\u52a8\u6001\u8c03\u7528\u3001\u4f7f\u7528<code>exec()<\/code>\u51fd\u6570\u6267\u884c\u4ee3\u7801\u3001\u5229\u7528<code>getattr()<\/code>\u51fd\u6570\u83b7\u53d6\u5e76\u8c03\u7528\u3002<\/strong>\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u6700\u5e38\u7528\u4e5f\u662f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u662f\u901a\u8fc7\u51fd\u6570\u540d\u76f4\u63a5\u8c03\u7528\u3002\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u7136\u540e\u901a\u8fc7\u51fd\u6570\u540d\u52a0\u62ec\u53f7\u7684\u65b9\u5f0f\u8fdb\u884c\u8c03\u7528\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u51fd\u6570\u540d\u76f4\u63a5\u8c03\u7528<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u51fd\u6570\u662f\u4e00\u7b49\u516c\u6c11\uff0c\u53ef\u4ee5\u50cf\u53d8\u91cf\u4e00\u6837\u88ab\u8d4b\u503c\u3001\u4f20\u9012\u548c\u8c03\u7528\u3002\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u540d\u76f4\u63a5\u8c03\u7528\u5b83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<h2><strong>\u4f7f\u7528\u51fd\u6570\u540d\u76f4\u63a5\u8c03\u7528<\/strong><\/h2>\n<p>print(greet(&quot;Alice&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>greet<\/code>\u7684\u51fd\u6570\uff0c\u5e76\u901a\u8fc7<code>greet(&quot;Alice&quot;)<\/code>\u6765\u8c03\u7528\u5b83\u3002\u8fd9\u79cd\u65b9\u5f0f\u7b80\u5355\u76f4\u89c2\uff0c\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u8c03\u7528\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u901a\u8fc7\u5b57\u7b26\u4e32\u540d\u79f0\u52a8\u6001\u8c03\u7528<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u6839\u636e\u5b57\u7b26\u4e32\u540d\u79f0\u52a8\u6001\u8c03\u7528\u51fd\u6570\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u548c<code>globals()<\/code>\u6216<code>locals()<\/code>\u51fd\u6570\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u5b57\u5178<\/li>\n<\/ol>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06\u51fd\u6570\u5b58\u50a8\u5728\u5b57\u5178\u4e2d\uff0c\u5e76\u901a\u8fc7\u5b57\u7b26\u4e32\u952e\u6765\u8c03\u7528\u5b83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>def subtract(a, b):<\/p>\n<p>    return a - b<\/p>\n<h2><strong>\u5c06\u51fd\u6570\u5b58\u50a8\u5728\u5b57\u5178\u4e2d<\/strong><\/h2>\n<p>operations = {<\/p>\n<p>    &quot;add&quot;: add,<\/p>\n<p>    &quot;subtract&quot;: subtract<\/p>\n<p>}<\/p>\n<h2><strong>\u52a8\u6001\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>result = operations[&quot;add&quot;](5, 3)<\/p>\n<p>print(result)  # \u8f93\u51fa: 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528<code>globals()<\/code>\u6216<code>locals()<\/code><\/li>\n<\/ol>\n<p><p><code>globals()<\/code>\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5168\u5c40\u53d8\u91cf\u7684\u5b57\u5178\uff0c\u800c<code>locals()<\/code>\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5c40\u90e8\u53d8\u91cf\u7684\u5b57\u5178\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e9b\u51fd\u6570\u6765\u52a8\u6001\u8c03\u7528\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def multiply(a, b):<\/p>\n<p>    return a * b<\/p>\n<h2><strong>\u52a8\u6001\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>func_name = &quot;multiply&quot;<\/p>\n<p>result = globals()[func_name](4, 2)<\/p>\n<p>print(result)  # \u8f93\u51fa: 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>exec()<\/code>\u51fd\u6570\u6267\u884c\u4ee3\u7801<\/p>\n<\/p>\n<p><p><code>exec()<\/code>\u51fd\u6570\u53ef\u4ee5\u6267\u884c\u52a8\u6001\u521b\u5efa\u7684\u4ee3\u7801\u3002\u867d\u7136\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u9700\u8981\u8c28\u614e\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5f15\u5165\u5b89\u5168\u98ce\u9669\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def divide(a, b):<\/p>\n<p>    return a \/ b<\/p>\n<h2><strong>\u4f7f\u7528exec()\u6267\u884c\u4ee3\u7801<\/strong><\/h2>\n<p>exec(&quot;result = divide(10, 2)&quot;)<\/p>\n<p>print(result)  # \u8f93\u51fa: 5.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>exec()<\/code>\u6267\u884c\u4e86\u4e00\u6bb5\u4ee3\u7801\uff0c\u8fd9\u6bb5\u4ee3\u7801\u8c03\u7528\u4e86<code>divide<\/code>\u51fd\u6570\u3002\u5c3d\u7ba1<code>exec()<\/code>\u63d0\u4f9b\u4e86\u7075\u6d3b\u6027\uff0c\u4f46\u901a\u5e38\u5efa\u8bae\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\uff0c\u9664\u975e\u7edd\u5bf9\u5fc5\u8981\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5229\u7528<code>getattr()<\/code>\u51fd\u6570\u83b7\u53d6\u5e76\u8c03\u7528<\/p>\n<\/p>\n<p><p><code>getattr()<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u4ece\u5bf9\u8c61\u4e2d\u83b7\u53d6\u5c5e\u6027\u6216\u65b9\u6cd5\u3002\u5bf9\u4e8e\u6a21\u5757\u6216\u7c7b\u4e2d\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>getattr()<\/code>\u6765\u83b7\u53d6\u5e76\u8c03\u7528\u5b83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Calculator:<\/p>\n<p>    def power(self, base, exponent):<\/p>\n<p>        return base  exponent<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61<\/strong><\/h2>\n<p>calc = Calculator()<\/p>\n<h2><strong>\u4f7f\u7528getattr()\u52a8\u6001\u8c03\u7528\u65b9\u6cd5<\/strong><\/h2>\n<p>method_name = &quot;power&quot;<\/p>\n<p>result = getattr(calc, method_name)(2, 3)<\/p>\n<p>print(result)  # \u8f93\u51fa: 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Calculator<\/code>\u7c7b\uff0c\u5e76\u4f7f\u7528<code>getattr()<\/code>\u52a8\u6001\u8c03\u7528\u5176<code>power<\/code>\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u5728\u7c7b\u5b9e\u4f8b\u4e2d\u52a8\u6001\u8c03\u7528\u65b9\u6cd5\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8fd0\u884c\u6307\u5b9a\u51fd\u6570\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u6bcf\u79cd\u65b9\u5f0f\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u51fd\u6570\u540d\u76f4\u63a5\u8c03\u7528<\/strong>\uff1a\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002<\/li>\n<li><strong>\u901a\u8fc7\u5b57\u7b26\u4e32\u540d\u79f0\u52a8\u6001\u8c03\u7528<\/strong>\uff1a\u5f53\u51fd\u6570\u540d\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\u65f6\uff0c\u4f7f\u7528\u5b57\u5178\u6216<code>globals()\/locals()<\/code>\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/li>\n<li><strong>\u4f7f\u7528<code>exec()<\/code>\u51fd\u6570<\/strong>\uff1a\u63d0\u4f9b\u4e86\u6267\u884c\u52a8\u6001\u4ee3\u7801\u7684\u80fd\u529b\uff0c\u4f46\u5e94\u8c28\u614e\u4f7f\u7528\u4ee5\u907f\u514d\u5b89\u5168\u98ce\u9669\u3002<\/li>\n<li><strong>\u5229\u7528<code>getattr()<\/code>\u51fd\u6570<\/strong>\uff1a\u9002\u7528\u4e8e\u5728\u5bf9\u8c61\u4e2d\u52a8\u6001\u8c03\u7528\u65b9\u6cd5\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n<p><p>\u5728\u9009\u62e9\u65b9\u6cd5\u65f6\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u5b89\u5168\u6027\u8003\u8651\u505a\u51fa\u51b3\u7b56\u3002\u5982\u679c\u9700\u6c42\u7b80\u5355\u4e14\u51fd\u6570\u540d\u79f0\u5df2\u77e5\uff0c\u4f18\u5148\u9009\u62e9\u51fd\u6570\u540d\u76f4\u63a5\u8c03\u7528\u3002\u5982\u679c\u9700\u8981\u52a8\u6001\u8c03\u7528\uff0c\u5c3d\u91cf\u907f\u514d\u4f7f\u7528<code>exec()<\/code>\uff0c\u800c\u662f\u4f7f\u7528\u5b57\u5178\u6216<code>getattr()<\/code>\u7b49\u66f4\u5b89\u5168\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528\u7279\u5b9a\u7684\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8c03\u7528\u4e00\u4e2a\u7279\u5b9a\u7684\u51fd\u6570\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ea\u9700\u786e\u4fdd\u51fd\u6570\u5df2\u7ecf\u5b9a\u4e49\uff0c\u5e76\u901a\u8fc7\u51fd\u6570\u540d\u79f0\u52a0\u4e0a\u62ec\u53f7\u6765\u8c03\u7528\u5b83\u3002\u5982\u679c\u51fd\u6570\u6709\u53c2\u6570\uff0c\u60a8\u9700\u8981\u5728\u62ec\u53f7\u5185\u4f20\u5165\u76f8\u5e94\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u540d\u4e3a<code>greet<\/code>\u7684\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u8c03\u7528\u5b83\uff1a<code>greet()<\/code>\uff0c\u5982\u679c\u5b83\u9700\u8981\u4e00\u4e2a\u53c2\u6570\uff0c\u6bd4\u5982<code>name<\/code>\uff0c\u5219\u5e94\u8be5\u8fd9\u6837\uff1a<code>greet(&quot;Alice&quot;)<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8fd0\u884c\u4e0d\u540c\u6a21\u5757\u4e2d\u7684\u51fd\u6570\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u60f3\u8981\u8fd0\u884c\u4e00\u4e2a\u5728\u4e0d\u540c\u6a21\u5757\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\u3002\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u540d\u4e3a<code>my_module.py<\/code>\u7684\u6587\u4ef6\uff0c\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>my_function<\/code>\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>import my_module<\/code>\u6765\u5bfc\u5165\u5b83\uff0c\u7136\u540e\u4f7f\u7528<code>my_module.my_function()<\/code>\u6765\u8c03\u7528\u8be5\u51fd\u6570\u3002\u786e\u4fdd\u6a21\u5757\u5728Python\u7684\u641c\u7d22\u8def\u5f84\u4e2d\u3002<\/p>\n<p><strong>\u5982\u4f55\u8c03\u8bd5Python\u51fd\u6570\u7684\u6267\u884c\uff1f<\/strong><br \/>\u8c03\u8bd5Python\u51fd\u6570\u7684\u6267\u884c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>print<\/code>\u8bed\u53e5\u6216\u4f7f\u7528Python\u5185\u7f6e\u7684<code>pdb<\/code>\u6a21\u5757\u6765\u5b8c\u6210\u3002<code>print<\/code>\u8bed\u53e5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u8ffd\u8e2a\u51fd\u6570\u7684\u8f93\u5165\u548c\u8f93\u51fa\uff0c\u800c<code>pdb<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u6bd4\u5982\u8bbe\u7f6e\u65ad\u70b9\u3001\u9010\u6b65\u6267\u884c\u4ee3\u7801\u7b49\u3002\u4f7f\u7528<code>import pdb; pdb.set_trace()<\/code>\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u8bbe\u7f6e\u65ad\u70b9\uff0c\u4ee5\u4fbf\u9010\u6b65\u68c0\u67e5\u4ee3\u7801\u6267\u884c\u8fc7\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8fd0\u884c\u6307\u5b9a\u51fd\u6570\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u51fd\u6570\u540d\u76f4\u63a5\u8c03\u7528\u3001\u901a\u8fc7\u5b57\u7b26\u4e32\u540d\u79f0\u52a8\u6001\u8c03\u7528\u3001\u4f7f\u7528exec()\u51fd\u6570\u6267\u884c\u4ee3\u7801\u3001\u5229 [&hellip;]","protected":false},"author":3,"featured_media":1008980,"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\/1008971"}],"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=1008971"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008971\/revisions"}],"predecessor-version":[{"id":1008983,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008971\/revisions\/1008983"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1008980"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1008971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1008971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1008971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}