{"id":2645,"date":"2022-02-22T14:48:03","date_gmt":"2022-02-22T06:48:03","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2645"},"modified":"2022-02-22T14:48:05","modified_gmt":"2022-02-22T06:48:05","slug":"python-global-nonlocal","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/02\/22\/python-global-nonlocal\/","title":{"rendered":"python \u5168\u5c40\u53d8\u91cf global \u548c nonlocal"},"content":{"rendered":"\n<p>      \u4e24\u4e2a\u5173\u952e\u8bcd\u90fd\u7528\u4e8e\u5141\u8bb8\u5728\u4e00\u4e2a\u5c40\u90e8<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=%E4%BD%9C%E7%94%A8%E5%9F%9F&amp;spm=1001.2101.3001.7020\" target=\"_blank\">\u4f5c\u7528\u57df<\/a>\u4e2d\u4f7f\u7528\u5916\u5c42\u7684\u53d8\u91cf\u3002<\/p>\n\n\n\n<ul><li>global \u8868\u793a\u5c06\u53d8\u91cf\u58f0\u660e\u4e3a\u5168\u5c40\u53d8\u91cf<\/li><li>nonlocal \u8868\u793a\u5c06\u53d8\u91cf\u58f0\u660e\u4e3a\u5916\u5c42\u53d8\u91cf\uff08\u5916\u5c42\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u800c\u4e14\u4e0d\u80fd\u662f\u5168\u5c40\u53d8\u91cf\uff09<\/li><\/ul>\n\n\n\n<ul><li>python\u5f15\u7528\u53d8\u91cf\u7684\u987a\u5e8f\u5982\u4e0b\uff1a<\/li><\/ul>\n\n\n\n<ol><li>\u5f53\u524d\u4f5c\u7528\u57df\u5c40\u90e8\u53d8\u91cf<\/li><li>\u5916\u5c42\u4f5c\u7528\u57df\u53d8\u91cf<\/li><li>\u5f53\u524d\u6a21\u5757\u4e2d\u7684\u5168\u5c40\u53d8\u91cf<\/li><li>python\u5185\u7f6e\u53d8\u91cf<\/li><\/ol>\n\n\n\n<p>\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a=1\ndef test(x):\n   global x\n   a=x\n\u56e0\u4e3aa\u5916\u5c42\u53d8\u91cf\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u6240\u4ee5\u8981\u52a0global\n\n\ndef test2():\n  a=1\n  def test(x)\n     nonlocal x\n     a=x\n\u56e0\u4e3aa\u5916\u5c42\u53d8\u91cf\u4e0d\u662f\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u4e5f\u662f\u4e00\u4e2a\u5916\u5c42\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u6240\u4ee5\u8981\u52a0nonlocal\n<\/code><\/pre>\n\n\n\n<p>python \u5728\u65b0\u5efa\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u9ed8\u8ba4\u4f5c\u7528\u57df\u4e3a\u5f53\u524d\u5c40\u90e8\u4f5c\u7528\u57df\u3002\u5982\u679c\u4e0d\u58f0\u660eglobal or nonglocal\uff0c\u5c31\u8ba4\u4e3a\u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\u3002<\/p>\n\n\n\n<p>\u5185\u90e8\u51fd\u6570\uff0c\u4e0d\u4fee\u6539\u5916\u5c42\u53d8\u91cf\u53ef\u4ee5\u8bbf\u95ee\u5916\u5c42\u53d8\u91cf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 1\ndef fun():\n    print(a) # \u5728\u51fd\u6570\u5185\u90e8\u627e\u4e0d\u5230\u5bf9 a \u7684\u5b9a\u4e49\uff0c\u5219\u53bb\u5916\u5c42\u67e5\u8be2\u3002\u8f93\u51fa1\u3002\nfun()<\/code><\/pre>\n\n\n\n<p>\u5185\u90e8\u51fd\u6570\uff0c\u4fee\u6539\u540c\u540d\u5916\u5c42\u53d8\u91cf\uff0c\u5219python\u4f1a\u8ba4\u4e3a\u662f\u5b9a\u4e49\u4e86\u4e00\u4e2a\u65b0\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u4e0e\u5916\u5c42\u53d8\u91cf\u65e0\u5173\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 1\r\ndef fun():\r\na = 2 # \u58f0\u660e\u4e86\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u4e0e\u5916\u9762\u7b49\u4e8e1\u7684\u90a3\u4e2aa\u6ca1\u6709\u5173\u7cfb\u4e86\r\nprint(a) # \u8f93\u51fa2\r\nfun()\r\nprint(a) # \u8f93\u51fa1<\/code><\/pre>\n\n\n\n<p>\u5728\u5185\u90e8\u51fd\u6570\u4fee\u6539\u540c\u540d\u5168\u5c40\u53d8\u91cf\u4e4b\u524d\u8c03\u7528\u53d8\u91cf\u540d\u79f0\uff0c\u5219\u5f15\u53d1Unbound-LocalError<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 1\r\ndef fun():\r\n    print(a) # \u5148\u5f15\u7528\r\n    a = 2 # \u518d\u4fee\u6539\r\nfun()<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u60f3\u8981\u5f3a\u5236\u8bbf\u95ee\u5916\u5c42\u53d8\u91cf a\uff0c\u4fbf\u53ef\u4ee5\u4f7f\u7528\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=global&amp;spm=1001.2101.3001.7020\" target=\"_blank\">global<\/a>\u00a0\u5173\u952e\u5b57<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 1\r\ndef fun():\r\n    global a # a\u4e3a\u5168\u5c40\u53d8\u91cf\r\n    print(a) # \u8f93\u51fa1\r\n    a = 2 # \u6539\u53d8\u7684\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u56e0\u6b64\u51fa\u4e86\u8fd9\u4e2a\u5c40\u90e8\u4f5c\u7528\u57df\uff0c\u4ecd\u7136\u6709\u6548\r\nfun()\r\nprint(a) # \u8f93\u51fa2\n\u6ce8\u610f\uff0c\u8fd9\u4e2a\u65f6\u5019\uff0c\u4e0d\u8981\u5c06 global \u6362\u4e3a nonlocal \u5173\u952e\u5b57\uff0c\u4e0d\u7136\u4f1a\u62a5\u9519\uff1a<\/code><\/pre>\n\n\n\n<p>\u8fd9\u662f\u56e0\u4e3a nonlocal \u8868\u793a\u5916\u5c42\u53d8\u91cf\uff0c\u4f46\u662f\u4e00\u65e6\u5916\u5c42\u53d8\u91cf\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u5219\u53ea\u80fd\u7528 global\u3002\u5982\u679c\u5c06\u8fd9\u6bb5\u4ee3\u7801\u5168\u90e8\u653e\u5230\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 nonlocal \uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def outer_fun():\r\na = 1\r\ndef fun():\r\nnonlocal a # a\u4e3a\u5916\u5c42\u53d8\u91cf\r\nprint(a) # \u8f93\u51fa1\r\na = 2\r\nfun()\r\nprint(a) #\u8f93\u51fa2\r\nouter_fun()<\/code><\/pre>\n\n\n\n<p>\u6ce8\u610f\uff1a\u53ef\u4ee5\u53d1\u73b0\uff0c\u4e4b\u524d\u7684\u5c06 global \u6362\u4e3a nonlocal \u7684\u62a5\u9519\uff0c\u548c\u8fd9\u6b21\u7684\u5c06 nonlocal \u6539\u4e3a global \u7684\u62a5\u9519\uff0c\u9519\u8bef\u7c7b\u578b\u662f\u4e0d\u540c\u7684\u3002\u524d\u8005\u662f nonlocal a \u8fd9\u4e00\u884c\u62a5\u9519\uff0c\u540e\u8005\u662f print(a) \u8fd9\u4e00\u884c\u62a5\u9519\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u4f7f\u7528 nonlocal a \u4e4b\u524d\uff0c\u5fc5\u987b\u4fdd\u8bc1\u5916\u5c42\u7684\u786e\u5df2\u7ecf\u5b9a\u4e49\u8fc7 a \u4e86\uff0c\u4f46\u662f\u5728 global a \u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u5141\u8bb8\u5168\u5c40\u53d8\u91cf\u4e2d\u8fd8\u6ca1\u6709\u5b9a\u4e49\u8fc7a\uff0c\u53ef\u4ee5\u7559\u5728\u540e\u9762\u5b9a\u4e49\u3002\u6bd4\u5982\u5c06 print(a) \u4e4b\u524d\u52a0\u4e0a\u5bf9a\u7684\u5b9a\u4e49\uff0c\u4fbf\u4e0d\u4f1a\u51fa\u9519\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def outer_fun():\r\na = 1\r\ndef fun():\r\nglobal a # a\u4e3a\u5168\u5c40\u53d8\u91cf\uff0c\u4e0e\u4e0a\u9762\u7b49\u4e8e1\u7684 a \u6ca1\u6709\u5173\u7cfb\r\na = 3 # \u5b9a\u4e49\u5168\u5c40\u53d8\u91cf\r\nprint(a) # \u8f93\u51fa3\r\na = 2\r\nfun()\r\nprint(a) #\u8f93\u51fa1\uff0c\u5c40\u90e8\u53d8\u91cf\r\nouter_fun()\r\nprint(a) # \u8f93\u51fa2\uff0c\u5168\u5c40\u53d8\u91cf<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e24\u4e2a\u5173\u952e\u8bcd\u90fd\u7528\u4e8e\u5141\u8bb8\u5728\u4e00\u4e2a\u5c40\u90e8\u4f5c\u7528\u57df\u4e2d\u4f7f\u7528\u5916\u5c42\u7684\u53d8\u91cf\u3002 global \u8868\u793a\u5c06\u53d8\u91cf\u58f0\u660e\u4e3a\u5168\u5c40\u53d8\u91cf nonloca &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/02\/22\/python-global-nonlocal\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">python \u5168\u5c40\u53d8\u91cf global \u548c nonlocal<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2645"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2645"}],"version-history":[{"count":21,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2645\/revisions"}],"predecessor-version":[{"id":2666,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2645\/revisions\/2666"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2645"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}