{"id":177074,"date":"2020-04-23T11:46:13","date_gmt":"2020-04-23T03:46:13","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=177074"},"modified":"2020-04-18T10:47:18","modified_gmt":"2020-04-18T02:47:18","slug":"python-built-in","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-built-in.html","title":{"rendered":"Python \u5185\u7f6e\u51fd\u6570:\u2014\u2014locals \u548c globals\u4ecb\u7ecd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nlocals \u548c globals\u8fd9\u4e24\u4e2a\u51fd\u6570\u4e3b\u8981\u63d0\u4f9b\uff0c\u57fa\u4e8e\u5b57\u5178\u7684\u8bbf\u95ee\u5c40\u90e8\u548c\u5168\u5c40\u53d8\u91cf\u7684\u65b9\u5f0f\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u5728\u7406\u89e3\u8fd9\u4e24\u4e2a\u51fd\u6570\u65f6\uff0c\u9996\u5148\u6765\u7406\u89e3\u4e00\u4e0b Python \u4e2d\u7684\u540d\u5b57\u7a7a\u95f4\u6982\u5ff5\u3002Python \u4f7f\u7528\u53eb\u505a\u540d\u5b57\u7a7a\u95f4\u7684\u4e1c\u897f\u6765\u8bb0\u5f55\u53d8\u91cf\u7684\u8f68\u8ff9\u3002\u540d\u5b57\u7a7a\u95f4\u53ea\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5b83\u7684\u952e\u5b57\u5c31\u662f\u53d8\u91cf\u540d\uff0c\u5b57\u5178\u7684\u503c\u5c31\u662f\u90a3\u4e9b\u53d8\u91cf\u7684\u503c\u3002<\/p>\n

\u5b9e\u9645\u4e0a\uff0c\u540d\u5b57\u7a7a\u95f4\u53ef\u4ee5\u50cf Python \u7684\u5b57\u5178\u4e00\u6837\u8fdb\u884c\u8bbf\u95ee\u3002<\/p>\n

\u6bcf\u4e2a\u51fd\u6570\u90fd\u6709\u7740\u81ea\u5df2\u7684\u540d\u5b57\u7a7a\u95f4\uff0c\u53eb\u505a\u5c40\u90e8\u540d\u5b57\u7a7a\u95f4\uff0c\u5b83\u8bb0\u5f55\u4e86\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u5305\u62ec\u51fd\u6570\u7684\u53c2\u6570\u548c\u5c40\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u3002\u6bcf\u4e2a\u6a21\u5757\u62e5\u6709\u5b83\u81ea\u5df2\u7684\u540d\u5b57\u7a7a\u95f4\uff0c\u53eb\u505a\u5168\u5c40\u540d\u5b57\u7a7a\u95f4\uff0c\u5b83\u8bb0\u5f55\u4e86\u6a21\u5757\u7684\u53d8\u91cf\uff0c\u5305\u62ec\u51fd\u6570\u3001\u7c7b\u3001\u5176\u5b83\u5bfc\u5165\u7684\u6a21\u5757\u3001\u6a21\u5757\u7ea7\u7684\u53d8\u91cf\u548c\u5e38\u91cf\u3002\u8fd8\u6709\u5c31\u662f\u5185\u7f6e\u540d\u5b57\u7a7a\u95f4\uff0c\u4efb\u4f55\u6a21\u5757\u5747\u53ef\u8bbf\u95ee\u5b83\uff0c\u5b83\u5b58\u653e\u7740\u5185\u7f6e\u7684\u51fd\u6570\u548c\u5f02\u5e38\u3002<\/p>\n

\u5f53\u4e00\u884c\u4ee3\u7801\u8981\u4f7f\u7528\u53d8\u91cf x \u7684\u503c\u65f6\uff0cPython \u4f1a\u5230\u6240\u6709\u53ef\u7528\u7684\u540d\u5b57\u7a7a\u95f4\u53bb\u67e5\u627e\u53d8\u91cf\uff0c\u6309\u7167\u5982\u4e0b\u987a\u5e8f\uff1a<\/p>\n

    \n
  1. \u5c40\u90e8\u540d\u5b57\u7a7a\u95f4 - \u7279\u6307\u5f53\u524d\u51fd\u6570\u6216\u7c7b\u7684\u65b9\u6cd5\u3002\u5982\u679c\u51fd\u6570\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf x\uff0cPython\u5c06\u4f7f\u7528\u8fd9\u4e2a\u53d8\u91cf\uff0c\u7136\u540e\u505c\u6b62\u641c\u7d22\u3002<\/li>\n
  2. \u5168\u5c40\u540d\u5b57\u7a7a\u95f4 - \u7279\u6307\u5f53\u524d\u7684\u6a21\u5757\u3002\u5982\u679c\u6a21\u5757\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a x \u7684\u53d8\u91cf\uff0c\u51fd\u6570\u6216\u7c7b\uff0cPython\u5c06\u4f7f\u7528\u8fd9\u4e2a\u53d8\u91cf\u7136\u540e\u505c\u6b62\u641c\u7d22\u3002<\/li>\n
  3. \u5185\u7f6e\u540d\u5b57\u7a7a\u95f4 - \u5bf9\u6bcf\u4e2a\u6a21\u5757\u90fd\u662f\u5168\u5c40\u7684\u3002\u4f5c\u4e3a\u6700\u540e\u7684\u5c1d\u8bd5\uff0cPython \u5c06\u5047\u8bbe x \u662f\u5185\u7f6e\u51fd\u6570\u6216\u53d8\u91cf\u3002<\/li>\n<\/ol>\n

    \u5982\u679c Python \u5728\u8fd9\u4e9b\u540d\u5b57\u7a7a\u95f4\u627e\u4e0d\u5230 x\uff0c\u5b83\u5c06\u653e\u5f03\u67e5\u627e\u5e76\u5f15\u53d1\u4e00\u4e2a NameError \u7684\u5f02\u5e38\uff0c\u540c\u65f6\u4f20\u9012 There is no variable named 'x' \u8fd9\u6837\u4e00\u6761\u4fe1\u606f\u3002<\/p>\n

    \u5c40\u90e8\u53d8\u91cf\u51fd\u6570 locals \u4f8b\u5b50\uff08locals \u8fd4\u56de\u4e00\u4e2a\u540d\u5b57\/\u503c\u5bf9\u7684\u5b57\u5178\uff09\uff1a<\/p>\n

    \u5b9e\u4f8b<\/p>\n

    def foo(arg, a):\r\n    x = 1\r\n    y = 'xxxxxx'\r\n    for i in range(10):\r\n        j = 1\r\n        k = i\r\n    print(locals())\r\n#\u8c03\u7528\u51fd\u6570\u7684\u6253\u5370\u7ed3\u679c    \r\nfoo(1,2)\r\n#{'k': 9, 'j': 1, 'i': 9, 'y': 'xxxxxx', 'x': 1, 'a': 2, 'arg': 1}<\/pre>\n

    from module import<\/strong> \u548c import module <\/strong>\u4e4b\u95f4\u7684\u4e0d\u540c\u3002\u4f7f\u7528 import module\uff0c\u6a21\u5757\u81ea\u8eab\u88ab\u5bfc\u5165\uff0c\u4f46\u662f\u5b83\u4fdd\u6301\u7740\u81ea\u5df2\u7684\u540d\u5b57\u7a7a\u95f4\uff0c\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u4f60\u9700\u8981\u4f7f\u7528\u6a21\u5757\u540d\u6765\u8bbf\u95ee\u5b83\u7684\u51fd\u6570\u6216\u5c5e\u6027\uff08module.function\uff09\u7684\u539f\u56e0\u3002\u4f46\u662f\u4f7f\u7528 from module import\uff0c\u5b9e\u9645\u4e0a\u662f\u4ece\u53e6\u4e00\u4e2a\u6a21\u5757\u4e2d\u5c06\u6307\u5b9a\u7684\u51fd\u6570\u548c\u5c5e\u6027\u5bfc\u5165\u5230\u4f60\u81ea\u5df1\u7684\u540d\u5b57\u7a7a\u95f4\uff0c\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u4f60\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5b83\u4eec\u5374\u4e0d\u9700\u8981\u5f15\u7528\u5b83\u4eec\u6240\u6765\u6e90\u7684\u6a21\u5757\u7684\u539f\u56e0\u3002<\/p>\n

    locals \u662f\u53ea\u8bfb\u7684\uff0cglobals \u4e0d\u662f\u3002<\/p>\n

    locals \u4e0d\u53ef\u4fee\u6539\uff0cglobals \u53ef\u4ee5\u4fee\u6539\uff0c\u539f\u56e0\u662f\uff1a<\/p>\n

      \n
    1. locals() \u5b9e\u9645\u4e0a\u6ca1\u6709\u8fd4\u56de\u5c40\u90e8\u540d\u5b57\u7a7a\u95f4\uff0c\u5b83\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u62f7\u8d1d\u3002\u6240\u4ee5\u5bf9\u5b83\u8fdb\u884c\u4fee\u6539\uff0c\u4fee\u6539\u7684\u662f\u62f7\u8d1d\uff0c\u800c\u5bf9\u5b9e\u9645\u7684\u5c40\u90e8\u540d\u5b57\u7a7a\u95f4\u4e2d\u7684\u53d8\u91cf\u503c\u5e76\u65e0\u5f71\u54cd\u3002<\/li>\n
    2. globals() \u8fd4\u56de\u7684\u662f\u5b9e\u9645\u7684\u5168\u5c40\u540d\u5b57\u7a7a\u95f4\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u62f7\u8d1d\u4e0e locals \u7684\u884c\u4e3a\u5b8c\u5168\u76f8\u53cd\u3002<\/li>\n<\/ol>\n

      \u6240\u4ee5\u5bf9 globals \u6240\u8fd4\u56de\u7684 dictionary \u7684\u4efb\u4f55\u7684\u6539\u52a8\u90fd\u4f1a\u76f4\u63a5\u5f71\u54cd\u5230\u5168\u5c40\u53d8\u91cf\u7684\u53d6\u503c\u3002<\/p>\n

      \u5b9e\u4f8b<\/p>\n

      #!\/usr\/bin\/env python    \r\n\r\nz = 7 #\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf  \r\ndef foo(arg):  \r\n    x = 1  \r\n    print( locals() )  \r\n    print('x=',x)\r\n    locals()['x'] = 2 #\u4fee\u6539\u7684\u662f\u5c40\u90e8\u540d\u5b57\u7a7a\u95f4\u7684\u62f7\u8d1d\uff0c\u800c\u5b9e\u9645\u7684\u5c40\u90e8\u540d\u5b57\u7a7a\u95f4\u4e2d\u7684\u53d8\u91cf\u503c\u5e76\u65e0\u5f71\u54cd\u3002  \r\n    print( locals() )\r\n    print( \"x=\",x )\r\n \r\nfoo(3)  \r\nprint( globals() )\r\nprint( 'z=',z )\r\nglobals()[\"z\"] = 8 #globals\uff08\uff09\u8fd4\u56de\u7684\u662f\u5b9e\u9645\u7684\u5168\u5c40\u540d\u5b57\u7a7a\u95f4\uff0c\u4fee\u6539\u53d8\u91cfz\u7684\u503c  \r\nprint( globals() )\r\nprint( \"z=\",z )<\/pre>\n

      \u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n

      {'x': 1, 'arg': 3}\r\nx= 1\r\n{'x': 1, 'arg': 3}\r\nx= 1\r\n{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10b099358>, '__spec__': None, '__annotations__': {}, '__builtins__': , '__file__': 'test.py', '__cached__': None, 'z': 7, 'foo': }\r\nz= 7\r\n{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10b099358>, '__spec__': None, '__annotations__': {}, '__builtins__': , '__file__': 'test.py', '__cached__': None, 'z': 8, 'foo': }\r\nz= 8<\/function><\/module><\/function><\/module><\/pre>\n","protected":false},"excerpt":{"rendered":"

      \u5728\u7406\u89e3\u8fd9\u4e24\u4e2a\u51fd\u6570\u65f6\uff0c\u9996\u5148\u6765\u7406\u89e3\u4e00\u4e0b Python \u4e2d\u7684\u540d\u5b57\u7a7a\u95f4\u6982\u5ff5\u3002Python \u4f7f\u7528\u53eb\u505a\u540d\u5b57\u7a7a\u95f4\u7684\u4e1c\u897f\u6765\u8bb0\u5f55 […]<\/p>\n","protected":false},"author":1470,"featured_media":177076,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-177074","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/177074","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=177074"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/177074\/revisions"}],"predecessor-version":[{"id":177079,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/177074\/revisions\/177079"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/177076"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=177074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=177074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=177074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}