{"id":1182042,"date":"2025-01-15T18:58:46","date_gmt":"2025-01-15T10:58:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1182042.html"},"modified":"2025-01-15T18:58:49","modified_gmt":"2025-01-15T10:58:49","slug":"%e5%a6%82%e4%bd%95%e4%bf%ae%e6%94%b9%e5%85%a8%e5%b1%80%e5%8f%98%e9%87%8fpython","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1182042.html","title":{"rendered":"\u5982\u4f55\u4fee\u6539\u5168\u5c40\u53d8\u91cfpython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25130152\/21a88ebf-4e11-460e-aa1e-cdca84ec68ad.webp\" alt=\"\u5982\u4f55\u4fee\u6539\u5168\u5c40\u53d8\u91cfpython\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528global\u5173\u952e\u5b57\u3001\u5c06\u5168\u5c40\u53d8\u91cf\u4f20\u9012\u5230\u51fd\u6570\u4e2d\u3001\u4f7f\u7528\u7c7b\u6216\u6a21\u5757\u5c01\u88c5\u5168\u5c40\u53d8\u91cf<\/strong>\u7b49\u65b9\u5f0f\u6765\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u5176\u4e2d\u4f7f\u7528global\u5173\u952e\u5b57\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002<strong>\u4f7f\u7528global\u5173\u952e\u5b57\u53ef\u4ee5\u8ba9\u51fd\u6570\u5185\u90e8\u76f4\u63a5\u8bbf\u95ee\u548c\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u800c\u65e0\u9700\u5c06\u5176\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4f7f\u7528global\u5173\u952e\u5b57<\/h3>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u5185\u90e8\uff0c\u5982\u679c\u8981\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528global\u5173\u952e\u5b57\u6765\u58f0\u660e\u8be5\u53d8\u91cf\u662f\u5168\u5c40\u53d8\u91cf\u3002\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\u51fd\u6570<code>modify_global<\/code>\u5185\u90e8\u4f7f\u7528\u4e86<code>global x<\/code>\uff0c\u8fd9\u4f7f\u5f97\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u5168\u5c40\u53d8\u91cf<code>x<\/code>\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528global\u5173\u952e\u5b57<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u5206\u4e3a\u5c40\u90e8\u4f5c\u7528\u57df\u548c\u5168\u5c40\u4f5c\u7528\u57df\u3002\u5f53\u5728\u51fd\u6570\u5185\u90e8\u58f0\u660e\u53d8\u91cf\u65f6\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u8be5\u53d8\u91cf\u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u4e0d\u80fd\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c\u3002\u5982\u679c\u5e0c\u671b\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">y = 5<\/p>\n<p>def change_global():<\/p>\n<p>    global y<\/p>\n<p>    y = 15<\/p>\n<p>change_global()<\/p>\n<p>print(y)  # \u8f93\u51fa: 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570<code>change_global<\/code>\u5185\u90e8\u4f7f\u7528\u4e86<code>global y<\/code>\uff0c\u8fd9\u4f7f\u5f97\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u5168\u5c40\u53d8\u91cf<code>y<\/code>\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u591a\u4e2a\u5168\u5c40\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u540c\u65f6\u58f0\u660e\u591a\u4e2a\u5168\u5c40\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 1<\/p>\n<p>b = 2<\/p>\n<p>def modify_globals():<\/p>\n<p>    global a, b<\/p>\n<p>    a = 3<\/p>\n<p>    b = 4<\/p>\n<p>modify_globals()<\/p>\n<p>print(a, b)  # \u8f93\u51fa: 3 4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570<code>modify_globals<\/code>\u5185\u90e8\u4f7f\u7528\u4e86<code>global a, b<\/code>\uff0c\u8fd9\u4f7f\u5f97\u51fd\u6570\u53ef\u4ee5\u540c\u65f6\u4fee\u6539\u5168\u5c40\u53d8\u91cf<code>a<\/code>\u548c<code>b<\/code>\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5c06\u5168\u5c40\u53d8\u91cf\u4f20\u9012\u5230\u51fd\u6570\u4e2d<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u65b9\u6cd5\u662f\u5c06\u5168\u5c40\u53d8\u91cf\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u5230\u51fd\u6570\u4e2d\u8fdb\u884c\u4fee\u6539\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\uff0c\u4ee3\u7801\u4e5f\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u901a\u8fc7\u53c2\u6570\u4f20\u9012<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u5168\u5c40\u53d8\u91cf\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u7136\u540e\u5728\u51fd\u6570\u5185\u90e8\u8fdb\u884c\u4fee\u6539\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">z = [10]<\/p>\n<p>def modify_list(lst):<\/p>\n<p>    lst[0] = 20<\/p>\n<p>modify_list(z)<\/p>\n<p>print(z)  # \u8f93\u51fa: [20]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5168\u5c40\u53d8\u91cf<code>z<\/code>\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5c06\u5176\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570<code>modify_list<\/code>\uff0c\u7136\u540e\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u53ef\u53d8\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5168\u5c40\u53d8\u91cf\u662f\u4e00\u4e2a\u53ef\u53d8\u5bf9\u8c61\uff08\u4f8b\u5982\u5217\u8868\u3001\u5b57\u5178\u7b49\uff09\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u8be5\u5bf9\u8c61\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">global_dict = {&#39;key&#39;: &#39;value&#39;}<\/p>\n<p>def modify_dict(d):<\/p>\n<p>    d[&#39;key&#39;] = &#39;new_value&#39;<\/p>\n<p>modify_dict(global_dict)<\/p>\n<p>print(global_dict)  # \u8f93\u51fa: {&#39;key&#39;: &#39;new_value&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5168\u5c40\u53d8\u91cf<code>global_dict<\/code>\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5c06\u5176\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570<code>modify_dict<\/code>\uff0c\u7136\u540e\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5b57\u5178\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7c7b\u5c01\u88c5\u5168\u5c40\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u7c7b\u6765\u5c01\u88c5\u5168\u5c40\u53d8\u91cf\u3002\u901a\u8fc7\u7c7b\u7684\u5c5e\u6027\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbf\u95ee\u548c\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u7c7b\u5c01\u88c5\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u5c06\u5168\u5c40\u53d8\u91cf\u4f5c\u4e3a\u7c7b\u7684\u5c5e\u6027\u8fdb\u884c\u5c01\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class GlobalVars:<\/p>\n<p>    value = 10<\/p>\n<p>def modify_class_var():<\/p>\n<p>    GlobalVars.value = 20<\/p>\n<p>modify_class_var()<\/p>\n<p>print(GlobalVars.value)  # \u8f93\u51fa: 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b<code>GlobalVars<\/code>\uff0c\u5c06\u5168\u5c40\u53d8\u91cf<code>value<\/code>\u4f5c\u4e3a\u7c7b\u7684\u5c5e\u6027\uff0c\u7136\u540e\u901a\u8fc7\u51fd\u6570<code>modify_class_var<\/code>\u4fee\u6539\u7c7b\u5c5e\u6027\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u5b9e\u4f8b\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u901a\u8fc7\u7c7b\u7684\u5b9e\u4f8b\u5c5e\u6027\u6765\u5c01\u88c5\u548c\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class GlobalVars:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.value = 10<\/p>\n<p>global_instance = GlobalVars()<\/p>\n<p>def modify_instance_var(instance):<\/p>\n<p>    instance.value = 20<\/p>\n<p>modify_instance_var(global_instance)<\/p>\n<p>print(global_instance.value)  # \u8f93\u51fa: 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b<code>GlobalVars<\/code>\uff0c\u5c06\u5168\u5c40\u53d8\u91cf<code>value<\/code>\u4f5c\u4e3a\u5b9e\u4f8b\u5c5e\u6027\uff0c\u7136\u540e\u901a\u8fc7\u51fd\u6570<code>modify_instance_var<\/code>\u4fee\u6539\u5b9e\u4f8b\u5c5e\u6027\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u6a21\u5757\u5c01\u88c5\u5168\u5c40\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u6a21\u5757\u6765\u5c01\u88c5\u5168\u5c40\u53d8\u91cf\u3002\u901a\u8fc7\u6a21\u5757\u7684\u5c5e\u6027\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbf\u95ee\u548c\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u6a21\u5757\u6587\u4ef6\uff0c\u4f8b\u5982<code>global_vars.py<\/code>\uff0c\u5728\u6a21\u5757\u4e2d\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># global_vars.py<\/p>\n<p>value = 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5728\u4e3b\u7a0b\u5e8f\u4e2d\u5bfc\u5165\u6a21\u5757\u5e76\u4fee\u6539\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5728\u4e3b\u7a0b\u5e8f\u4e2d\u5bfc\u5165\u6a21\u5757\uff0c\u5e76\u901a\u8fc7\u6a21\u5757\u7684\u5c5e\u6027\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import global_vars<\/p>\n<p>def modify_module_var():<\/p>\n<p>    global_vars.value = 20<\/p>\n<p>modify_module_var()<\/p>\n<p>print(global_vars.value)  # \u8f93\u51fa: 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u5bfc\u5165\u6a21\u5757<code>global_vars<\/code>\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbf\u95ee\u548c\u4fee\u6539\u6a21\u5757\u4e2d\u7684\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u907f\u514d\u547d\u540d\u51b2\u7a81<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u65f6\uff0c\u9700\u8981\u907f\u514d\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u7684\u547d\u540d\u51b2\u7a81\u3002\u5982\u679c\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u540c\u540d\uff0c\u51fd\u6570\u5185\u90e8\u4f1a\u4f18\u5148\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\uff0c\u800c\u4e0d\u4f1a\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>def modify_var():<\/p>\n<p>    x = 20  # \u8fd9\u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u4e0d\u4f1a\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c<\/p>\n<p>modify_var()<\/p>\n<p>print(x)  # \u8f93\u51fa: 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570<code>modify_var<\/code>\u5185\u90e8\u58f0\u660e\u4e86\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf<code>x<\/code>\uff0c\u6240\u4ee5\u4e0d\u4f1a\u4fee\u6539\u5168\u5c40\u53d8\u91cf<code>x<\/code>\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u7684\u53ef\u8bfb\u6027<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u4f1a\u589e\u52a0\u4ee3\u7801\u7684\u8026\u5408\u5ea6\uff0c\u964d\u4f4e\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u56e0\u6b64\uff0c\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u5e94\u5c3d\u91cf\u51cf\u5c11\u5168\u5c40\u53d8\u91cf\u7684\u4f7f\u7528\u3002\u5982\u679c\u5fc5\u987b\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\uff0c\u53ef\u4ee5\u901a\u8fc7\u7c7b\u6216\u6a21\u5757\u8fdb\u884c\u5c01\u88c5\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u7ebf\u7a0b\u5b89\u5168<\/h4>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u65f6\u9700\u8981\u6ce8\u610f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u548c\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u7ade\u4e89\u548c\u4e0d\u4e00\u81f4\u6027\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u7ebf\u7a0b\u9501\uff08<code>threading.Lock<\/code>\uff09\u6765\u786e\u4fdd\u5168\u5c40\u53d8\u91cf\u7684\u4fee\u6539\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>x = 10<\/p>\n<p>lock = threading.Lock()<\/p>\n<p>def modify_var_thread_<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>():<\/p>\n<p>    global x<\/p>\n<p>    with lock:<\/p>\n<p>        x = 20<\/p>\n<p>threads = []<\/p>\n<p>for _ in range(5):<\/p>\n<p>    t = threading.Thread(target=modify_var_thread_safe)<\/p>\n<p>    threads.append(t)<\/p>\n<p>    t.start()<\/p>\n<p>for t in threads:<\/p>\n<p>    t.join()<\/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\u901a\u8fc7\u4f7f\u7528\u7ebf\u7a0b\u9501\uff0c\u786e\u4fdd\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u4fee\u6539\u5168\u5c40\u53d8\u91cf<code>x<\/code>\u65f6\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u6848\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u4e0b\u9762\u901a\u8fc7\u4e00\u4e2a\u5b9e\u9645\u5e94\u7528\u6848\u4f8b\u6765\u5c55\u793a\u4e0d\u540c\u65b9\u6cd5\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6848\u4f8b\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u6570\u5668\u7a0b\u5e8f\uff0c\u9700\u8981\u5728\u591a\u4e2a\u51fd\u6570\u4e2d\u5171\u4eab\u548c\u4fee\u6539\u8ba1\u6570\u5668\u7684\u503c\u3002\u6211\u4eec\u5c06\u5c55\u793a\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u3001\u5c06\u5168\u5c40\u53d8\u91cf\u4f20\u9012\u5230\u51fd\u6570\u4e2d\u3001\u4f7f\u7528\u7c7b\u6216\u6a21\u5757\u5c01\u88c5\u5168\u5c40\u53d8\u91cf\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528global\u5173\u952e\u5b57<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">count = 0<\/p>\n<p>def increment():<\/p>\n<p>    global count<\/p>\n<p>    count += 1<\/p>\n<p>def decrement():<\/p>\n<p>    global count<\/p>\n<p>    count -= 1<\/p>\n<p>increment()<\/p>\n<p>increment()<\/p>\n<p>decrement()<\/p>\n<p>print(count)  # \u8f93\u51fa: 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\uff0c\u51fd\u6570<code>increment<\/code>\u548c<code>decrement<\/code>\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u5168\u5c40\u53d8\u91cf<code>count<\/code>\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5c06\u5168\u5c40\u53d8\u91cf\u4f20\u9012\u5230\u51fd\u6570\u4e2d<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">count = [0]<\/p>\n<p>def increment(counter):<\/p>\n<p>    counter[0] += 1<\/p>\n<p>def decrement(counter):<\/p>\n<p>    counter[0] -= 1<\/p>\n<p>increment(count)<\/p>\n<p>increment(count)<\/p>\n<p>decrement(count)<\/p>\n<p>print(count[0])  # \u8f93\u51fa: 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u5c06\u5168\u5c40\u53d8\u91cf<code>count<\/code>\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570<code>increment<\/code>\u548c<code>decrement<\/code>\uff0c\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u4f7f\u7528\u7c7b\u5c01\u88c5\u5168\u5c40\u53d8\u91cf<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">class Counter:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.count = 0<\/p>\n<p>global_counter = Counter()<\/p>\n<p>def increment(counter):<\/p>\n<p>    counter.count += 1<\/p>\n<p>def decrement(counter):<\/p>\n<p>    counter.count -= 1<\/p>\n<p>increment(global_counter)<\/p>\n<p>increment(global_counter)<\/p>\n<p>decrement(global_counter)<\/p>\n<p>print(global_counter.count)  # \u8f93\u51fa: 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u7c7b<code>Counter<\/code>\uff0c\u5c06\u5168\u5c40\u53d8\u91cf<code>count<\/code>\u4f5c\u4e3a\u5b9e\u4f8b\u5c5e\u6027\u8fdb\u884c\u5c01\u88c5\uff0c\u7136\u540e\u5728\u51fd\u6570<code>increment<\/code>\u548c<code>decrement<\/code>\u4e2d\u4fee\u6539\u5b9e\u4f8b\u5c5e\u6027\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h4>5\u3001\u4f7f\u7528\u6a21\u5757\u5c01\u88c5\u5168\u5c40\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u6a21\u5757\u6587\u4ef6<code>counter.py<\/code>\uff0c\u5728\u6a21\u5757\u4e2d\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># counter.py<\/p>\n<p>count = 0<\/p>\n<p>def increment():<\/p>\n<p>    global count<\/p>\n<p>    count += 1<\/p>\n<p>def decrement():<\/p>\n<p>    global count<\/p>\n<p>    count -= 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e3b\u7a0b\u5e8f\u4e2d\u5bfc\u5165\u6a21\u5757\uff0c\u5e76\u8c03\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import counter<\/p>\n<p>counter.increment()<\/p>\n<p>counter.increment()<\/p>\n<p>counter.decrement()<\/p>\n<p>print(counter.count)  # \u8f93\u51fa: 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u5bfc\u5165\u6a21\u5757<code>counter<\/code>\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbf\u95ee\u548c\u4fee\u6539\u6a21\u5757\u4e2d\u7684\u5168\u5c40\u53d8\u91cf<code>count<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u3001\u5c06\u5168\u5c40\u53d8\u91cf\u4f20\u9012\u5230\u51fd\u6570\u4e2d\u3001\u4f7f\u7528\u7c7b\u6216\u6a21\u5757\u5c01\u88c5\u5168\u5c40\u53d8\u91cf\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5b9e\u9645\u9700\u6c42\u548c\u7f16\u7a0b\u4e60\u60ef\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528global\u5173\u952e\u5b57<\/strong>\uff1a\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u4f46\u53ef\u80fd\u4f1a\u589e\u52a0\u4ee3\u7801\u7684\u8026\u5408\u5ea6\u548c\u590d\u6742\u6027\u3002<\/li>\n<li><strong>\u5c06\u5168\u5c40\u53d8\u91cf\u4f20\u9012\u5230\u51fd\u6570\u4e2d<\/strong>\uff1a\u53ef\u4ee5\u907f\u514d\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\uff0c\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u53d8\u91cf\u7684\u53ef\u53d8\u6027\u3002<\/li>\n<li><strong>\u4f7f\u7528\u7c7b\u5c01\u88c5\u5168\u5c40\u53d8\u91cf<\/strong>\uff1a\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u4f46\u9700\u8981\u5b9a\u4e49\u7c7b\u548c\u5b9e\u4f8b\u3002<\/li>\n<li><strong>\u4f7f\u7528\u6a21\u5757\u5c01\u88c5\u5168\u5c40\u53d8\u91cf<\/strong>\uff1a\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ba1\u7406\u5168\u5c40\u53d8\u91cf\uff0c\u4f46\u9700\u8981\u989d\u5916\u521b\u5efa\u6a21\u5757\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u6ce8\u610f\u907f\u514d\u547d\u540d\u51b2\u7a81\u3001\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\u3002\u901a\u8fc7\u5408\u7406\u5730\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u548c\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u8d28\u91cf\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf\uff1f<\/strong><br \/>\u5168\u5c40\u53d8\u91cf\u662f\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u6574\u4e2a\u6a21\u5757\u4e2d\u8bbf\u95ee\u3002\u60a8\u53ea\u9700\u5728\u51fd\u6570\u5916\u90e8\u521b\u5efa\u53d8\u91cf\uff0c\u5e76\u53ef\u4ee5\u5728\u4efb\u4f55\u51fd\u6570\u4e2d\u8bbf\u95ee\u5b83\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">x = 10  # \u5168\u5c40\u53d8\u91cf\n\ndef my_function():\n    print(x)\n\nmy_function()  # \u8f93\u51fa: 10\n<\/code><\/pre>\n<p>\u8981\u5728\u51fd\u6570\u4e2d\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u9700\u8981\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u3002<\/p>\n<p><strong>\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u65f6\u6709\u4ec0\u4e48\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u58f0\u660e\u7684\u53d8\u91cf\u5728\u51fd\u6570\u4e2d\u53ef\u4ee5\u88ab\u4fee\u6539\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>global<\/code>\u5173\u952e\u5b57\u53ea\u80fd\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u3002\u5982\u679c\u5728\u51fd\u6570\u4e2d\u6ca1\u6709\u4f7f\u7528<code>global<\/code>\uff0c\u5219\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u800c\u4e0d\u662f\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">x = 10\n\ndef my_function():\n    global x\n    x = 20  # \u4fee\u6539\u5168\u5c40\u53d8\u91cf\n\nmy_function()\nprint(x)  # \u8f93\u51fa: 20\n<\/code><\/pre>\n<p><strong>\u5168\u5c40\u53d8\u91cf\u4f1a\u5f71\u54cd\u7a0b\u5e8f\u7684\u53ef\u7ef4\u62a4\u6027\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u8fc7\u591a\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u548c\u8c03\u8bd5\u3002\u56e0\u4e3a\u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u5728\u7a0b\u5e8f\u7684\u4efb\u4f55\u5730\u65b9\u88ab\u4fee\u6539\uff0c\u8fd9\u4f7f\u5f97\u8ffd\u8e2a\u5176\u72b6\u6001\u53d8\u5f97\u590d\u6742\u3002\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u5efa\u8bae\u5c3d\u91cf\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\uff0c\u5e76\u901a\u8fc7\u53c2\u6570\u4f20\u9012\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528global\u5173\u952e\u5b57\u3001\u5c06\u5168\u5c40\u53d8\u91cf\u4f20\u9012\u5230\u51fd\u6570\u4e2d\u3001\u4f7f\u7528\u7c7b\u6216\u6a21\u5757\u5c01\u88c5\u5168\u5c40\u53d8\u91cf\u7b49\u65b9\u5f0f\u6765\u4fee\u6539 [&hellip;]","protected":false},"author":3,"featured_media":1182051,"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\/1182042"}],"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=1182042"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182042\/revisions"}],"predecessor-version":[{"id":1182056,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182042\/revisions\/1182056"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1182051"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1182042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1182042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1182042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}