{"id":977551,"date":"2024-12-27T06:30:33","date_gmt":"2024-12-26T22:30:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/977551.html"},"modified":"2024-12-27T06:30:35","modified_gmt":"2024-12-26T22:30:35","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e5%85%a8%e5%b1%80%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/977551.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4e49\u5168\u5c40\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24202831\/b23c4943-e90c-4c47-92f3-65bead9086cd.webp\" alt=\"python\u5982\u4f55\u5b9a\u4e49\u5168\u5c40\u51fd\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b9a\u4e49\u5168\u5c40\u51fd\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u5728\u6a21\u5757\u7684\u6700\u5916\u5c42\u5b9a\u4e49\u51fd\u6570\u3001\u4f7f\u7528global\u5173\u952e\u5b57\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf\u3001\u786e\u4fdd\u5728\u51fd\u6570\u5185\u90e8\u4e0d\u91cd\u65b0\u5b9a\u4e49\u53d8\u91cf\u7b49\u65b9\u6cd5\u5b9e\u73b0\u3002\u5168\u5c40\u51fd\u6570\u53ef\u4ee5\u5728\u540c\u4e00\u6a21\u5757\u7684\u4efb\u4f55\u5730\u65b9\u8c03\u7528\uff0c\u5e76\u4e14\u5728\u5176\u4ed6\u6a21\u5757\u4e2d\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165\u7684\u65b9\u5f0f\u4f7f\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><p>Python\u662f\u4e00\u79cd\u975e\u5e38\u7075\u6d3b\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5176\u51fd\u6570\u5b9a\u4e49\u548c\u53d8\u91cf\u4f5c\u7528\u57df\u7684\u5904\u7406\u4e5f\u4f53\u73b0\u4e86\u8fd9\u4e00\u70b9\u3002\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u5168\u5c40\u51fd\u6570\u7684\u5173\u952e\u5728\u4e8e\u51fd\u6570\u7684\u4f4d\u7f6e\u548c\u53d8\u91cf\u4f5c\u7528\u57df\u7684\u7ba1\u7406\u3002\u901a\u5e38\uff0c\u5168\u5c40\u51fd\u6570\u662f\u5728\u6a21\u5757\u7684\u6700\u5916\u5c42\u5b9a\u4e49\u7684\uff0c\u8fd9\u6837\u5b9a\u4e49\u7684\u51fd\u6570\u53ef\u4ee5\u88ab\u6a21\u5757\u5185\u7684\u5176\u4ed6\u51fd\u6570\u548c\u7c7b\u76f4\u63a5\u8c03\u7528\uff0c\u4e5f\u53ef\u4ee5\u88ab\u5176\u4ed6\u6a21\u5757\u901a\u8fc7\u5bfc\u5165\u8fdb\u884c\u8c03\u7528\u3002\u4e3a\u4e86\u786e\u4fdd\u51fd\u6570\u7684\u5168\u5c40\u6027\uff0c\u5c3d\u91cf\u907f\u514d\u5728\u51fd\u6570\u5185\u90e8\u91cd\u65b0\u5b9a\u4e49\u540c\u540d\u53d8\u91cf\uff0c\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u5c40\u90e8\u53d8\u91cf\u5bf9\u5168\u5c40\u53d8\u91cf\u7684\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001Python\u4e2d\u7684\u5168\u5c40\u51fd\u6570\u5b9a\u4e49<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5168\u5c40\u51fd\u6570\u901a\u5e38\u662f\u5728\u811a\u672c\u6216\u6a21\u5757\u7684\u6700\u5916\u5c42\u5b9a\u4e49\u7684\u3002\u8fd9\u6837\u5b9a\u4e49\u7684\u51fd\u6570\u53ef\u4ee5\u5728\u6a21\u5757\u7684\u5176\u4ed6\u90e8\u5206\u88ab\u8c03\u7528\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165\u6a21\u5757\u5728\u5176\u4ed6\u811a\u672c\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def global_function():<\/p>\n<p>    print(&quot;This is a global function&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u662f\u6700\u5e38\u7528\u7684\u5b9a\u4e49\u5168\u5c40\u51fd\u6570\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u660e\u4e86\uff0c\u5e76\u4e14\u9075\u5faaPython\u7684\u6a21\u5757\u5316\u8bbe\u8ba1\u7406\u5ff5\u3002\u5168\u5c40\u51fd\u6570\u7684\u5b9a\u4e49\u4f4d\u7f6e\u901a\u5e38\u5728\u6a21\u5757\u7684\u9876\u5c42\uff0c\u8fd9\u6837\u5728\u6a21\u5757\u52a0\u8f7d\u65f6\u8fd9\u4e9b\u51fd\u6570\u5c31\u5df2\u7ecf\u88ab\u5b9a\u4e49\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5168\u5c40\u53d8\u91cf\u4e0eglobal\u5173\u952e\u5b57<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4e3a\u4e86\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u3002\u5728\u51fd\u6570\u5185\u90e8\u901a\u8fc7<code>global<\/code>\u58f0\u660e\u7684\u53d8\u91cf\uff0c\u5728\u51fd\u6570\u5916\u90e8\u4e5f\u80fd\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>def modify_global():<\/p>\n<p>    global x<\/p>\n<p>    x = 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7<code>global<\/code>\u5173\u952e\u5b57\uff0c\u51fd\u6570<code>modify_global<\/code>\u53ef\u4ee5\u4fee\u6539\u5168\u5c40\u53d8\u91cf<code>x<\/code>\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fc7\u5ea6\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u4f1a\u5bfc\u81f4\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u4e0b\u964d\uff0c\u56e0\u6b64\u5e94\u5f53\u8c28\u614e\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u8de8\u6a21\u5757\u4f7f\u7528\u5168\u5c40\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u5728\u4e0d\u540c\u6a21\u5757\u4e4b\u95f4\u5171\u4eab\u5168\u5c40\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6a21\u5757\u7684\u5bfc\u5165\u6765\u5b9e\u73b0\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u6a21\u5757<code>module_a<\/code>\uff0c\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module_a.py<\/p>\n<p>def shared_function():<\/p>\n<p>    print(&quot;This function can be used across modules&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u53e6\u4e00\u4e2a\u6a21\u5757\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165<code>module_a<\/code>\u6765\u4f7f\u7528<code>shared_function<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module_b.py<\/p>\n<p>import module_a<\/p>\n<p>module_a.shared_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5229\u7528\u4e86Python\u7684\u6a21\u5757\u548c\u5305\u673a\u5236\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u548c\u53ef\u91cd\u7528\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u907f\u514d\u5c40\u90e8\u53d8\u91cf\u8986\u76d6\u5168\u5c40\u53d8\u91cf<\/p>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u4e0e\u5168\u5c40\u53d8\u91cf\u540c\u540d\u7684\u53d8\u91cf\uff0c\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u5e76\u4e0d\u4f1a\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u660e\u786e\u58f0\u660e\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">y = 50<\/p>\n<p>def change_y():<\/p>\n<p>    global y<\/p>\n<p>    y = 100<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u6ca1\u6709\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\uff0c\u51fd\u6570<code>change_y<\/code>\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5c40\u90e8\u53d8\u91cf<code>y<\/code>\uff0c\u800c\u4e0d\u4f1a\u5f71\u54cd\u5230\u5168\u5c40\u7684<code>y<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6700\u4f73\u5b9e\u8df5\u4e0e\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5c3d\u91cf\u51cf\u5c11\u5168\u5c40\u53d8\u91cf\u7684\u4f7f\u7528<\/strong>\uff1a\u5168\u5c40\u53d8\u91cf\u4f1a\u5bfc\u81f4\u51fd\u6570\u4e4b\u95f4\u7684\u4f9d\u8d56\u6027\u589e\u52a0\uff0c\u4ece\u800c\u964d\u4f4e\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u6a21\u5757\u548c\u5305<\/strong>\uff1a\u901a\u8fc7\u6a21\u5757\u548c\u5305\u7684\u65b9\u5f0f\u7ec4\u7ec7\u4ee3\u7801\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63a7\u5236\u51fd\u6570\u548c\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u4f7f\u7528\u4e0e\u5185\u7f6e\u51fd\u6570\u76f8\u540c\u7684\u540d\u79f0<\/strong>\uff1a\u5728\u5b9a\u4e49\u5168\u5c40\u51fd\u6570\u65f6\uff0c\u907f\u514d\u4f7f\u7528\u4e0ePython\u5185\u7f6e\u51fd\u6570\u76f8\u540c\u7684\u540d\u79f0\uff0c\u4ee5\u514d\u5bfc\u81f4\u610f\u5916\u884c\u4e3a\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6e05\u6670\u7684\u547d\u540d<\/strong>\uff1a\u4e3a\u5168\u5c40\u51fd\u6570\u548c\u53d8\u91cf\u9009\u62e9\u5177\u6709\u63cf\u8ff0\u6027\u7684\u540d\u79f0\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6587\u6863\u5316<\/strong>\uff1a\u4e3a\u5168\u5c40\u51fd\u6570\u548c\u53d8\u91cf\u7f16\u5199\u8be6\u7ec6\u7684\u6587\u6863\uff0c\u4ee5\u4fbf\u4e8e\u5176\u4ed6\u5f00\u53d1\u8005\u7406\u89e3\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5408\u7406\u5730\u5b9a\u4e49\u548c\u4f7f\u7528\u5168\u5c40\u51fd\u6570\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u7ec4\u7ec7\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002\u7136\u800c\uff0c\u8fc7\u5ea6\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u548c\u51fd\u6570\u4e5f\u4f1a\u5e26\u6765\u4e00\u4e9b\u95ee\u9898\uff0c\u56e0\u6b64\u5728\u8bbe\u8ba1\u7a0b\u5e8f\u65f6\u5e94\u5f53\u6743\u8861\u5229\u5f0a\uff0c\u9009\u62e9\u5408\u9002\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u5168\u5c40\u51fd\u6570\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ea\u9700\u5728\u4efb\u4f55\u51fd\u6570\u6216\u7c7b\u4e4b\u5916\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\u6765\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">def my_global_function():\n    print(&quot;\u8fd9\u662f\u4e00\u4e2a\u5168\u5c40\u51fd\u6570&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u88ab\u8c03\u7528\uff0c\u53ea\u8981\u5728\u5b83\u88ab\u8c03\u7528\u4e4b\u524d\u5df2\u7ecf\u5b9a\u4e49\u3002<\/p>\n<p><strong>\u5168\u5c40\u51fd\u6570\u4e0e\u5c40\u90e8\u51fd\u6570\u6709\u4ec0\u4e48\u4e0d\u540c\uff1f<\/strong><br \/>\u5168\u5c40\u51fd\u6570\u662f\u5728\u6240\u6709\u4f5c\u7528\u57df\u4e2d\u53ef\u8bbf\u95ee\u7684\uff0c\u800c\u5c40\u90e8\u51fd\u6570\u53ea\u80fd\u5728\u5176\u5b9a\u4e49\u7684\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u3002\u5c40\u90e8\u51fd\u6570\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\u5728\u5176\u4ed6\u51fd\u6570\u5185\u90e8\uff0c\u5b83\u7684\u4f5c\u7528\u57df\u4ec5\u9650\u4e8e\u5916\u90e8\u51fd\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">def outer_function():\n    def inner_function():\n        print(&quot;\u8fd9\u662f\u4e00\u4e2a\u5c40\u90e8\u51fd\u6570&quot;)\n    inner_function()  # \u53ef\u4ee5\u5728\u8fd9\u91cc\u8c03\u7528\n\nouter_function()\n# inner_function()  # \u8fd9\u5c06\u5bfc\u81f4\u9519\u8bef\uff0c\u56e0\u4e3a\u5b83\u662f\u5c40\u90e8\u7684\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u4f5c\u7528\u57df\u7684\u533a\u522b\u5bf9\u4e8e\u4ee3\u7801\u7684\u7ec4\u7ec7\u548c\u53ef\u7ef4\u62a4\u6027\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5168\u5c40\u51fd\u6570\u4e2d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\uff1f<\/strong><br \/>\u5728\u5168\u5c40\u51fd\u6570\u4e2d\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5168\u5c40\u53d8\u91cf\u3002\u5982\u679c\u60a8\u9700\u8981\u5728\u5168\u5c40\u51fd\u6570\u4e2d\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u6765\u58f0\u660e\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">global_variable = 10\n\ndef modify_global():\n    global global_variable\n    global_variable += 5\n    print(global_variable)\n\nmodify_global()  # \u8f93\u51fa 15\nprint(global_variable)  # \u8f93\u51fa 15\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u786e\u4fdd\u60a8\u6b63\u5728\u4fee\u6539\u7684\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5c40\u90e8\u53d8\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9a\u4e49\u5168\u5c40\u51fd\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u5728\u6a21\u5757\u7684\u6700\u5916\u5c42\u5b9a\u4e49\u51fd\u6570\u3001\u4f7f\u7528global\u5173\u952e\u5b57\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf\u3001\u786e\u4fdd\u5728\u51fd [&hellip;]","protected":false},"author":3,"featured_media":977560,"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\/977551"}],"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=977551"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977551\/revisions"}],"predecessor-version":[{"id":977561,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977551\/revisions\/977561"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/977560"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=977551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=977551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=977551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}