{"id":1044826,"date":"2024-12-31T13:17:59","date_gmt":"2024-12-31T05:17:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1044826.html"},"modified":"2024-12-31T13:18:02","modified_gmt":"2024-12-31T05:18:02","slug":"python%e5%87%bd%e6%95%b0%e5%86%85%e9%83%a8%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%85%a8%e5%b1%80%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1044826.html","title":{"rendered":"python\u51fd\u6570\u5185\u90e8\u5982\u4f55\u4f7f\u7528\u5168\u5c40\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/f62680a7-bc56-4bd4-aaff-4db1716f3d47.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u51fd\u6570\u5185\u90e8\u5982\u4f55\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u3001\u901a\u8fc7\u53c2\u6570\u4f20\u9012\u5168\u5c40\u53d8\u91cf\u3001\u4ee5\u53ca\u4f7f\u7528\u6a21\u5757\u7ea7\u522b\u7684\u53d8\u91cf\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u3002\u8fd9\u79cd\u65b9\u6cd5\u80fd\u663e\u5f0f\u5730\u58f0\u660e\u67d0\u4e2a\u53d8\u91cf\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u4ece\u800c\u5728\u51fd\u6570\u5185\u90e8\u5bf9\u5176\u8fdb\u884c\u8bbf\u95ee\u548c\u4fee\u6539\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u76f8\u5173\u7684\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>global<\/code> \u5173\u952e\u5b57<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u5728\u51fd\u6570\u5185\u90e8\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\u662f\u5168\u5c40\u53d8\u91cf\u3002\u5426\u5219\uff0cPython\u4f1a\u9ed8\u8ba4\u8ba4\u4e3a\u4f60\u5728\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u4e86\u4e00\u4e2a\u65b0\u7684\u5c40\u90e8\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10  # \u5168\u5c40\u53d8\u91cf<\/p>\n<p>def modify_global_variable():<\/p>\n<p>    global x  # \u58f0\u660e x \u662f\u5168\u5c40\u53d8\u91cf<\/p>\n<p>    x = 20  # \u4fee\u6539\u5168\u5c40\u53d8\u91cf x<\/p>\n<p>    print(f&quot;Inside function: x = {x}&quot;)<\/p>\n<p>modify_global_variable()<\/p>\n<p>print(f&quot;Outside function: x = {x}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u51fd\u6570<code>modify_global_variable<\/code>\u4f7f\u7528<code>global x<\/code>\u58f0\u660e<code>x<\/code>\u662f\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u5e76\u5728\u51fd\u6570\u5185\u90e8\u5bf9\u5176\u503c\u8fdb\u884c\u4e86\u4fee\u6539\u3002\u8fd0\u884c\u4ee3\u7801\u540e\uff0c\u8f93\u51fa\u7ed3\u679c\u4f1a\u663e\u793a<code>x<\/code>\u7684\u503c\u5728\u51fd\u6570\u5185\u90e8\u548c\u5916\u90e8\u90fd\u88ab\u4fee\u6539\u4e3a20\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u901a\u8fc7\u53c2\u6570\u4f20\u9012\u5168\u5c40\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u901a\u8fc7\u53c2\u6570\u5c06\u5168\u5c40\u53d8\u91cf\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5e0c\u671b\u51fd\u6570\u4fdd\u6301\u901a\u7528\u6027\uff0c\u4e14\u4e0d\u4f9d\u8d56\u4e8e\u5168\u5c40\u53d8\u91cf\u7684\u60c5\u51b5\u3002\u901a\u8fc7\u5c06\u5168\u5c40\u53d8\u91cf\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u53ef\u4ee5\u907f\u514d\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\uff0c\u540c\u65f6\u4e5f\u4f7f\u5f97\u51fd\u6570\u66f4\u52a0\u6613\u4e8e\u6d4b\u8bd5\u548c\u590d\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10  # \u5168\u5c40\u53d8\u91cf<\/p>\n<p>def modify_variable(var):<\/p>\n<p>    var = 20<\/p>\n<p>    print(f&quot;Inside function: var = {var}&quot;)<\/p>\n<p>    return var<\/p>\n<p>x = modify_variable(x)<\/p>\n<p>print(f&quot;Outside function: x = {x}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5168\u5c40\u53d8\u91cf<code>x<\/code>\u88ab\u4f20\u9012\u7ed9\u51fd\u6570<code>modify_variable<\/code>\uff0c\u5e76\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5176\u503c\u3002\u51fd\u6570\u8fd4\u56de\u4fee\u6539\u540e\u7684\u503c\uff0c\u5e76\u5c06\u5176\u91cd\u65b0\u8d4b\u503c\u7ed9\u5168\u5c40\u53d8\u91cf<code>x<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6a21\u5757\u7ea7\u522b\u7684\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u66f4\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u5728\u5355\u72ec\u7684\u6a21\u5757\u4e2d\uff0c\u5e76\u5728\u5176\u4ed6\u6a21\u5757\u4e2d\u5bfc\u5165\u548c\u4f7f\u7528\u8fd9\u4e9b\u53d8\u91cf\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5168\u5c40\u53d8\u91cf\u7684\u5b9a\u4e49\u548c\u4f7f\u7528\u5206\u79bb\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># globals.py<\/p>\n<p>x = 10  # \u6a21\u5757\u7ea7\u522b\u7684\u5168\u5c40\u53d8\u91cf<\/p>\n<h2><strong>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/strong><\/h2>\n<p>import globals<\/p>\n<p>def modify_global_variable():<\/p>\n<p>    globals.x = 20  # \u4fee\u6539\u6a21\u5757\u7ea7\u522b\u7684\u5168\u5c40\u53d8\u91cf<\/p>\n<p>    print(f&quot;Inside function: globals.x = {globals.x}&quot;)<\/p>\n<p>modify_global_variable()<\/p>\n<p>print(f&quot;Outside function: globals.x = {globals.x}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5168\u5c40\u53d8\u91cf<code>x<\/code>\u88ab\u5b9a\u4e49\u5728<code>globals<\/code>\u6a21\u5757\u4e2d\uff0c\u5e76\u5728<code>main<\/code>\u6a21\u5757\u4e2d\u901a\u8fc7\u5bfc\u5165<code>globals<\/code>\u6a21\u5757\u6765\u8bbf\u95ee\u548c\u4fee\u6539\u8be5\u53d8\u91cf\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u4e0d\u540c\u6a21\u5757\u4e4b\u95f4\u5171\u4eab\u5168\u5c40\u53d8\u91cf\uff0c\u540c\u65f6\u4fdd\u6301\u4ee3\u7801\u7ed3\u6784\u7684\u6e05\u6670\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u3001\u901a\u8fc7\u53c2\u6570\u4f20\u9012\u5168\u5c40\u53d8\u91cf\u3001\u4ee5\u53ca\u4f7f\u7528\u6a21\u5757\u7ea7\u522b\u7684\u53d8\u91cf\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u590d\u7528\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\uff1a<\/strong> \u9002\u7528\u4e8e\u7b80\u5355\u7684\u5168\u5c40\u53d8\u91cf\u8bbf\u95ee\u548c\u4fee\u6539\uff0c\u4f46\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u3002<\/li>\n<li><strong>\u901a\u8fc7\u53c2\u6570\u4f20\u9012\u5168\u5c40\u53d8\u91cf\uff1a<\/strong> \u9002\u7528\u4e8e\u5e0c\u671b\u51fd\u6570\u4fdd\u6301\u901a\u7528\u6027\u548c\u72ec\u7acb\u6027\u7684\u60c5\u51b5\uff0c\u907f\u514d\u4e86\u5bf9\u5168\u5c40\u53d8\u91cf\u7684\u76f4\u63a5\u4f9d\u8d56\u3002<\/li>\n<li><strong>\u4f7f\u7528\u6a21\u5757\u7ea7\u522b\u7684\u53d8\u91cf\uff1a<\/strong> \u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u5728\u5355\u72ec\u7684\u6a21\u5757\u4e2d\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/li>\n<\/ol>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u6ce8\u610f\u5168\u5c40\u53d8\u91cf\u7684\u4f7f\u7528\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4ee3\u7801\u7684\u8026\u5408\u6027\u589e\u52a0\uff0c\u5e94\u5c3d\u91cf\u907f\u514d\u5728\u51fd\u6570\u4e2d\u9891\u7e41\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\uff0c\u4ee5\u4fdd\u6301\u4ee3\u7801\u7684\u6a21\u5757\u5316\u548c\u72ec\u7acb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u51fd\u6570\u5185\u90e8\u8bbf\u95ee\u5168\u5c40\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u7684\u540d\u79f0\u5728\u51fd\u6570\u5185\u90e8\u8bbf\u95ee\u5b83\u4eec\u3002\u53ea\u8981\u786e\u4fdd\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u4e86\u8be5\u53d8\u91cf\uff0c\u51fd\u6570\u5c31\u80fd\u8bfb\u53d6\u5176\u503c\u3002\u4f8b\u5982\uff1a\u5982\u679c\u60a8\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf<code>x<\/code>\uff0c\u90a3\u4e48\u5728\u51fd\u6570\u5185\u90e8\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>x<\/code>\u6765\u83b7\u53d6\u5176\u503c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c\uff0c\u60a8\u9700\u8981\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u3002\u901a\u8fc7\u5728\u51fd\u6570\u5185\u90e8\u58f0\u660e<code>global variable_name<\/code>\uff0c\u60a8\u53ef\u4ee5\u544a\u8bc9Python\u8be5\u53d8\u91cf\u662f\u5168\u5c40\u7684\uff0c\u4ece\u800c\u5141\u8bb8\u60a8\u4fee\u6539\u5b83\u3002\u4f8b\u5982\uff1a&#8220;`python<br \/>\nx = 10<br \/>\ndef modify_global():<br \/>\n    global x<br \/>\n    x = 20<br \/>\nmodify_global()<br \/>\nprint(x)  # \u8f93\u51fa20<\/p>\n<pre><code>\n&lt;strong&gt;\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f&lt;\/strong&gt;  \n\u5c3d\u7ba1\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\uff0c\u4f46\u5efa\u8bae\u5c3d\u91cf\u51cf\u5c11\u8fd9\u79cd\u505a\u6cd5\u3002\u5168\u5c40\u53d8\u91cf\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u548c\u8c03\u8bd5\u3002\u4e00\u4e2a\u66f4\u597d\u7684\u505a\u6cd5\u662f\u5c06\u9700\u8981\u4f20\u9012\u7684\u6570\u636e\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u6b64\u5916\uff0c\u8003\u8651\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u6765\u7ba1\u7406\u72b6\u6001\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7ec4\u7ec7\u4ee3\u7801\u7ed3\u6784\u3002\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528global\u5173\u952e\u5b57\u3001\u901a\u8fc7\u53c2\u6570\u4f20\u9012\u5168\u5c40\u53d8\u91cf\u3001\u4ee5\u53ca\u4f7f\u7528\u6a21\u5757\u7ea7 [&hellip;]","protected":false},"author":3,"featured_media":1044838,"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\/1044826"}],"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=1044826"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1044826\/revisions"}],"predecessor-version":[{"id":1044841,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1044826\/revisions\/1044841"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1044838"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1044826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1044826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1044826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}