{"id":1016154,"date":"2024-12-27T12:14:03","date_gmt":"2024-12-27T04:14:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1016154.html"},"modified":"2024-12-27T12:14:05","modified_gmt":"2024-12-27T04:14:05","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89global%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1016154.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4e49global\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095813\/2a85f26a-ca0a-4669-9d2d-c57337287602.webp\" alt=\"python\u5982\u4f55\u5b9a\u4e49global\u53d8\u91cf\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5b9a\u4e49global\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u58f0\u660e\u53d8\u91cf\u4e3a\u5168\u5c40\u53d8\u91cf\u3001\u5728\u51fd\u6570\u5916\u76f4\u63a5\u5b9a\u4e49\u53d8\u91cf\u3001\u4f7f\u7528\u6a21\u5757\u7ea7\u522b\u53d8\u91cf\u8fd9\u4e09\u79cd\u65b9\u5f0f\u3002<\/strong>\u5728Python\u4e2d\uff0cglobal\u53d8\u91cf\u662f\u6307\u5728\u6574\u4e2a\u6a21\u5757\u6216\u811a\u672c\u4e2d\u90fd\u53ef\u4ee5\u8bbf\u95ee\u7684\u53d8\u91cf\u3002\u5728\u51fd\u6570\u6216\u5176\u4ed6\u4f5c\u7528\u57df\u4e2d\uff0c\u5982\u679c\u9700\u8981\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c\uff0c\u5fc5\u987b\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u6765\u58f0\u660e\u8be5\u53d8\u91cf\u4e3a\u5168\u5c40\u53d8\u91cf\uff0c\u5426\u5219Python\u4f1a\u5c06\u5176\u89c6\u4e3a\u5c40\u90e8\u53d8\u91cf\u3002\u6b64\u5916\uff0c\u5728\u6a21\u5757\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u4e5f\u53ef\u4ee5\u88ab\u89c6\u4e3a\u5168\u5c40\u53d8\u91cf\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u88ab\u5176\u4ed6\u6a21\u5757\u5bfc\u5165\u548c\u4f7f\u7528\u3002\u5728\u6b64\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u4e00\u4e0bPython\u4e2d\u5982\u4f55\u5b9a\u4e49\u548c\u4f7f\u7528global\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528 <code>global<\/code> \u5173\u952e\u5b57<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5982\u679c\u5728\u51fd\u6570\u5185\u90e8\u9700\u8981\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c\uff0c\u5fc5\u987b\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u6765\u58f0\u660e\u8be5\u53d8\u91cf\u4e3a\u5168\u5c40\u53d8\u91cf\u3002\u901a\u8fc7\u8fd9\u4e00\u65b9\u5f0f\uff0cPython\u4f1a\u77e5\u9053\u6211\u4eec\u662f\u5728\u5f15\u7528\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5c40\u90e8\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/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>modify_global()<\/p>\n<p>print(x)  # \u8f93\u51fa: 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728\u51fd\u6570<code>modify_global<\/code>\u4e2d\u4f7f\u7528<code>global x<\/code>\u58f0\u660e\u4e86<code>x<\/code>\u4e3a\u5168\u5c40\u53d8\u91cf\uff0c\u56e0\u6b64\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5176\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u53d8\u91cf<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4efb\u4f55\u5728\u51fd\u6570\u6216\u7c7b\u4e4b\u5916\u5b9a\u4e49\u7684\u53d8\u91cf\u90fd\u662f\u5168\u5c40\u53d8\u91cf\u3002\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u5728\u6574\u4e2a\u6a21\u5757\u4e2d\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">y = 30<\/p>\n<p>def print_global():<\/p>\n<p>    print(y)<\/p>\n<p>print_global()  # \u8f93\u51fa: 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf<code>y<\/code>\u662f\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u7684\uff0c\u56e0\u6b64\u5b83\u662f\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u51fd\u6570<code>print_global<\/code>\u4e2d\u76f4\u63a5\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u6a21\u5757\u7ea7\u522b\u53d8\u91cf<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u53d8\u91cf\u5b9a\u4e49\u5728\u6a21\u5757\u4e2d\uff0c\u4f7f\u5176\u6210\u4e3a\u5168\u5c40\u53d8\u91cf\u3002\u8fd9\u4e9b\u53d8\u91cf\u53ef\u4ee5\u5728\u5176\u4ed6\u6a21\u5757\u4e2d\u88ab\u5bfc\u5165\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u6709\u4e00\u4e2a\u6a21\u5757<code>config.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># config.py<\/p>\n<p>z = 50<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u53e6\u4e00\u4e2a\u6a21\u5757\u4e2d\uff0c\u53ef\u4ee5\u5bfc\u5165\u5e76\u4f7f\u7528<code>config.py<\/code>\u4e2d\u7684\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/p>\n<p>import config<\/p>\n<p>def print_module_global():<\/p>\n<p>    print(config.z)<\/p>\n<p>print_module_global()  # \u8f93\u51fa: 50<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6a21\u5757\u7ea7\u522b\u7684\u53d8\u91cf\u53ef\u4ee5\u5728\u6574\u4e2a\u9879\u76ee\u4e2d\u88ab\u4e0d\u540c\u7684\u6a21\u5757\u8bbf\u95ee\u548c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5168\u5c40\u53d8\u91cf\u7684\u4f7f\u7528\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u907f\u514d\u8fc7\u5ea6\u4f7f\u7528\u5168\u5c40\u53d8\u91cf<\/strong>\uff1a\u8fc7\u591a\u7684\u5168\u5c40\u53d8\u91cf\u4f1a\u4f7f\u4ee3\u7801\u96be\u4ee5\u7406\u89e3\u548c\u7ef4\u62a4\u3002\u5c3d\u91cf\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\uff0c\u53ea\u6709\u5728\u786e\u5b9e\u9700\u8981\u5728\u591a\u4e2a\u51fd\u6570\u4e4b\u95f4\u5171\u4eab\u6570\u636e\u65f6\u624d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<\/strong>\uff1a\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u5168\u5c40\u53d8\u91cf\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528\u7ebf\u7a0b\u9501\u6765\u786e\u4fdd\u591a\u4e2a\u7ebf\u7a0b\u4e0d\u4f1a\u540c\u65f6\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u547d\u540d\u51b2\u7a81<\/strong>\uff1a\u4e3a\u4e86\u907f\u514d\u547d\u540d\u51b2\u7a81\uff0c\u5efa\u8bae\u5168\u5c40\u53d8\u91cf\u4f7f\u7528\u7279\u5b9a\u7684\u547d\u540d\u89c4\u8303\uff0c\u5982\u4f7f\u7528\u5168\u5927\u5199\u5b57\u6bcd\u6216\u5728\u53d8\u91cf\u540d\u524d\u52a0\u7279\u5b9a\u524d\u7f00\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5982\u4f55\u67e5\u770b\u6240\u6709\u5168\u5c40\u53d8\u91cf<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>globals()<\/code>\u51fd\u6570\u67e5\u770b\u5f53\u524d\u6a21\u5757\u4e2d\u7684\u6240\u6709\u5168\u5c40\u53d8\u91cf\u3002\u8be5\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5168\u5c40\u53d8\u91cf\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 100<\/p>\n<p>b = 200<\/p>\n<p>def show_globals():<\/p>\n<p>    print(globals())<\/p>\n<p>show_globals()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u5c06\u8f93\u51fa\u5f53\u524d\u6a21\u5757\u4e2d\u6240\u6709\u7684\u5168\u5c40\u53d8\u91cf\u53ca\u5176\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5168\u5c40\u53d8\u91cf\u901a\u5e38\u7528\u4e8e\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u914d\u7f6e\u548c\u5e38\u91cf<\/strong>\uff1a\u5728\u6a21\u5757\u4e2d\u5b9a\u4e49\u914d\u7f6e\u53c2\u6570\u548c\u5e38\u91cf\uff0c\u4f7f\u5176\u53ef\u4ee5\u5728\u6574\u4e2a\u9879\u76ee\u4e2d\u88ab\u8bbf\u95ee\u3002\u4f8b\u5982\uff0c\u6570\u636e\u5e93\u8fde\u63a5\u914d\u7f6e\u3001API\u5bc6\u94a5\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8de8\u51fd\u6570\u5171\u4eab\u72b6\u6001<\/strong>\uff1a\u5f53\u591a\u4e2a\u51fd\u6570\u9700\u8981\u5171\u4eab\u67d0\u4e2a\u72b6\u6001\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u8ba1\u6570\u5668\u3001\u7d2f\u52a0\u5668\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6a21\u5757\u95f4\u901a\u4fe1<\/strong>\uff1a\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u4e0d\u540c\u6a21\u5757\u4e4b\u95f4\u9700\u8981\u5171\u4eab\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3\u8d77\u6765\uff0c\u5728Python\u4e2d\u5b9a\u4e49\u548c\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u975e\u5e38\u65b9\u4fbf\uff0c\u4f46\u4e5f\u9700\u8981\u8c28\u614e\u5bf9\u5f85\uff0c\u4ee5\u907f\u514d\u4ee3\u7801\u7ef4\u62a4\u56f0\u96be\u548c\u6f5c\u5728\u7684\u9519\u8bef\u3002\u5728\u8003\u8651\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u65f6\uff0c\u5e94\u6743\u8861\u5176\u5e26\u6765\u7684\u4fbf\u5229\u4e0e\u53ef\u80fd\u7684\u590d\u6742\u6027\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528global\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528global\u5173\u952e\u5b57\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u4f7f\u5176\u5728\u51fd\u6570\u5185\u5916\u90fd\u53ef\u4ee5\u8bbf\u95ee\u3002\u901a\u8fc7\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528global\u58f0\u660e\uff0c\u60a8\u53ef\u4ee5\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">x = 10  # \u5168\u5c40\u53d8\u91cf\n\ndef modify_global():\n    global x  # \u58f0\u660ex\u4e3a\u5168\u5c40\u53d8\u91cf\n    x += 5    # \u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c\n\nmodify_global()\nprint(x)  # \u8f93\u51fa15\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u51fd\u6570modify_global\u6210\u529f\u5730\u4fee\u6539\u4e86\u5168\u5c40\u53d8\u91cfx\u7684\u503c\u3002<\/p>\n<p><strong>global\u53d8\u91cf\u6709\u4ec0\u4e48\u4f18\u7f3a\u70b9\uff1f<\/strong><br \/>\u4f7f\u7528global\u53d8\u91cf\u7684\u4f18\u70b9\u5728\u4e8e\u53ef\u4ee5\u5728\u591a\u4e2a\u51fd\u6570\u4e4b\u95f4\u5171\u4eab\u6570\u636e\uff0c\u7b80\u5316\u4e86\u67d0\u4e9b\u7a0b\u5e8f\u7684\u7ed3\u6784\u3002\u7136\u800c\uff0c\u7f3a\u70b9\u662f\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u4e0b\u964d\uff0c\u56e0\u4e3a\u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u5728\u7a0b\u5e8f\u7684\u4efb\u4f55\u5730\u65b9\u88ab\u4fee\u6539\uff0c\u589e\u52a0\u4e86\u51fa\u9519\u7684\u98ce\u9669\u3002\u901a\u5e38\u5efa\u8bae\u5c3d\u91cf\u51cf\u5c11\u5bf9global\u53d8\u91cf\u7684\u4f7f\u7528\uff0c\u91c7\u7528\u53c2\u6570\u4f20\u9012\u6216\u8fd4\u56de\u503c\u7684\u65b9\u5f0f\u6765\u7ba1\u7406\u6570\u636e\u3002<\/p>\n<p><strong>\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u5e94\u8be5\u4f7f\u7528global\u53d8\u91cf\uff1f<\/strong><br \/>\u5f53\u67d0\u4e2a\u503c\u9700\u8981\u5728\u591a\u4e2a\u51fd\u6570\u4e4b\u95f4\u5171\u4eab\u5e76\u9891\u7e41\u4fee\u6539\u65f6\uff0c\u4f7f\u7528global\u53d8\u91cf\u53ef\u80fd\u4f1a\u66f4\u65b9\u4fbf\u3002\u4f8b\u5982\uff0c\u8ba1\u6570\u5668\u3001\u72b6\u6001\u6807\u5fd7\u6216\u914d\u7f6e\u8bbe\u7f6e\u7b49\u60c5\u51b5\u3002\u7136\u800c\uff0c\u4e3a\u4e86\u4fdd\u6301\u4ee3\u7801\u7684\u6e05\u6670\u548c\u53ef\u7ef4\u62a4\uff0c\u5e94\u8be5\u8c28\u614e\u4f7f\u7528\uff0c\u5e76\u8003\u8651\u662f\u5426\u6709\u5176\u4ed6\u66f4\u597d\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff08\u5982\u7c7b\u6216\u6a21\u5757\uff09\u6765\u7ba1\u7406\u8fd9\u4e9b\u5171\u4eab\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5b9a\u4e49global\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528global\u5173\u952e\u5b57\u58f0\u660e\u53d8\u91cf\u4e3a\u5168\u5c40\u53d8\u91cf\u3001\u5728\u51fd\u6570\u5916\u76f4\u63a5\u5b9a\u4e49\u53d8\u91cf\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1016163,"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\/1016154"}],"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=1016154"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1016154\/revisions"}],"predecessor-version":[{"id":1016166,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1016154\/revisions\/1016166"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1016163"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1016154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1016154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1016154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}