{"id":263,"date":"2017-02-06T23:33:20","date_gmt":"2017-02-06T14:33:20","guid":{"rendered":"http:\/\/www.python.ambitious-engineer.com\/?p=263"},"modified":"2018-01-15T23:39:03","modified_gmt":"2018-01-15T14:39:03","slug":"%e5%86%85%e9%83%a8%e9%96%a2%e6%95%b0%ef%bc%88inner-function%ef%bc%89%e3%81%a8nonlocal%e5%ae%a3%e8%a8%80","status":"publish","type":"post","link":"https:\/\/www.python.ambitious-engineer.com\/archives\/263","title":{"rendered":"\u5185\u90e8\u95a2\u6570\uff08inner function\uff09\u3068nonlocal\u5ba3\u8a00"},"content":{"rendered":"<h2>\u5185\u90e8\u95a2\u6570\uff08inner function\uff09<\/h2>\n<p>Python\u3067\u306f\u3001\u95a2\u6570\u5185\u90e8\u306b\u95a2\u6570\u3092\u5b9a\u7fa9\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002\u3053\u306e\u95a2\u6570\u3092\u5185\u90e8\u95a2\u6570\uff08inner function\uff09\u3068\u547c\u3073\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef outer_function():\r\n    &quot;&quot;&quot; \u5916\u5074\u306e\u95a2\u6570 &quot;&quot;&quot;\r\n    print('outer')\r\n\r\n    def inner_function():\r\n        &quot;&quot;&quot; \u5185\u5074\u306e\u95a2\u6570 &quot;&quot;&quot;\r\n        print('inner')\r\n\r\n    inner_function()\r\n\r\nouter_function()\r\n<\/pre>\n<p>\u5148\u307b\u3069\u306e\u95a2\u6570\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u8003\u3048\u65b9\u3092\u5fdc\u7528\u3059\u308b\u3068\u3001\u95a2\u6570\u306e\u623b\u308a\u5024\u3068\u3057\u3066\u5185\u90e8\u95a2\u6570\u3092\u8fd4\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u30b5\u30f3\u30d7\u30eb\u3067\u306f\u3001outer_function\u3092\u547c\u3073\u51fa\u3059\u3068\u623b\u308a\u5024\u306binner_function\u3092\u95a2\u6570\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u8fd4\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef outer_function():\r\n    &quot;&quot;&quot; \u5916\u5074\u306e\u95a2\u6570 &quot;&quot;&quot;\r\n    print('outer')\r\n\r\n    def inner_function():\r\n        &quot;&quot;&quot; \u5185\u5074\u306e\u95a2\u6570 &quot;&quot;&quot;\r\n        print('inner')\r\n\r\n    # \u5185\u5074\u306e\u95a2\u6570\u3092\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066return\u3059\u308b\r\n    f = inner_function\r\n    return f\r\n\r\nf = outer_function()\r\n# \u53d7\u3051\u53d6\u3063\u305f\u5185\u5074\u306e\u95a2\u6570\u3092\u5b9f\u884c\u3059\u308b\r\nf() # inner\u304c\u51fa\u529b\u3055\u308c\u308b\r\n\r\n<\/pre>\n<p>\u6700\u521d\u306f\u9055\u548c\u611f\u304c\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u300c\u95a2\u6570\u3092\u5b9f\u884c\u3059\u308b\u3068\u623b\u308a\u5024\u306b\u95a2\u6570\u304c\u5f97\u3089\u308c\u308b\u300d\u3068\u3044\u3046\u5b9f\u88c5\u306f\u69d8\u3005\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u5229\u7528\u3059\u308b\u969b\u306b\u898b\u3089\u308c\u308b\u306e\u3067\u77e5\u3063\u3066\u304a\u3044\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h2>nonlocal\u5ba3\u8a00<\/h2>\n<p>\u5185\u5074\u3067\u5b9a\u7fa9\u3057\u305f\u95a2\u6570\u304b\u3089\u5916\u5074\u306e\u30ed\u30fc\u30ab\u30eb\u95a2\u6570\u3092\u53c2\u7167\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002\u5185\u5074\u306e\u95a2\u6570\u3067\u5916\u5074\u306e\u30ed\u30fc\u30ab\u30eb\u5909\u6570\u3092\u5909\u66f4\u3057\u305f\u3044\u5834\u5408\u3001nonlocal\u5ba3\u8a00\u3092\u8a18\u8ff0\u3059\u308b\u3053\u3068\u3067\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002global\u5ba3\u8a00\u3068\u4f3c\u3066\u3044\u307e\u3059\u306d\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef outer_function():\r\n    &quot;&quot;&quot; \u5916\u5074\u306e\u95a2\u6570 &quot;&quot;&quot;\r\n\r\n    x = 100\r\n    print(x) # 100\u304c\u51fa\u529b\u3055\u308c\u308b\r\n\r\n    def inner_function():\r\n        &quot;&quot;&quot; \u5185\u5074\u306e\u95a2\u6570 &quot;&quot;&quot;\r\n        nonlocal x\r\n        x = 200\r\n        print(x) # 200\u304c\u51fa\u529b\u3055\u308c\u308b\r\n    \r\n    inner_function()\r\n    print(x) # 200\u304c\u51fa\u529b\u3055\u308c\u308b\r\n    \r\nouter_function()\r\n<\/pre>\n<p>\u5916\u5074\u306e\u95a2\u6570\u306e\u5909\u6570\u304c\u5185\u90e8\u95a2\u6570\u3067\u5909\u66f4\u3055\u308c\u305f\u3053\u3068\u304c\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<br \/>\nn<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5185\u90e8\u95a2\u6570\uff08inner function\uff09 Python\u3067\u306f\u3001\u95a2\u6570\u5185\u90e8\u306b\u95a2\u6570\u3092\u5b9a\u7fa9\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002\u3053\u306e\u95a2\u6570\u3092\u5185\u90e8\u95a2\u6570\uff08inner function\uff09\u3068\u547c\u3073\u307e\u3059\u3002 \u5148\u307b\u3069\u306e\u95a2\u6570\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u8003\u3048\u65b9\u3092\u5fdc\u7528\u3059\u308b\u3068\u3001\u95a2\u6570\u306e\u623b\u308a...<\/p>\n","protected":false},"author":1,"featured_media":3482,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[210],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/263"}],"collection":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/comments?post=263"}],"version-history":[{"count":3,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/263\/revisions"}],"predecessor-version":[{"id":3481,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/263\/revisions\/3481"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media\/3482"}],"wp:attachment":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/categories?post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/tags?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}