{"id":290,"date":"2019-01-08T03:30:18","date_gmt":"2019-01-07T18:30:18","guid":{"rendered":"http:\/\/10.124.124.111\/?p=290"},"modified":"2019-01-13T01:40:39","modified_gmt":"2019-01-12T16:40:39","slug":"python%e3%81%ae%e3%83%8d%e3%82%b9%e3%83%88%e3%81%95%e3%82%8c%e3%81%9f%e9%96%a2%e6%95%b0%e3%81%8b%e3%82%89%e5%a4%96%e9%83%a8%e3%81%ae%e5%a4%89%e6%95%b0%e3%81%ae%e5%80%a4%e3%82%92%e5%8f%82%e7%85%a7","status":"publish","type":"post","link":"https:\/\/blog.homi.run\/?p=290","title":{"rendered":"Python\u306e\u30cd\u30b9\u30c8\u3055\u308c\u305f\u95a2\u6570\u304b\u3089\u5916\u90e8\u306e\u5909\u6570\u306e\u5024\u3092\u53c2\u7167\u3059\u308b"},"content":{"rendered":"<p>inner function\u3092\u4f7f\u3046\u3053\u3068\u304c\u306a\u304b\u3063\u305f\u3081\u3061\u3087\u3063\u3068\u8a70\u307e\u3063\u305f\u306e\u3067\u30e1\u30e2\u7a0b\u5ea6\u306b\u3002<\/p>\n<h3>Inner Function\u3068\u306f<\/h3>\n<p>Python\u3067\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u95a2\u6570\u306e\u4e2d\u306b\u66f4\u306b\u95a2\u6570\u3092\u5b9a\u7fa9\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef method_a():\r\n    a = 100\r\n    print(a)  # 100\r\n\r\n    def method_b():\r\n        b = 200\r\n        print(b)  # 200\r\n\r\n<\/pre>\n<h3><\/h3>\n<p>&nbsp;<\/p>\n<h3>\u30cd\u30b9\u30c8\u3055\u308c\u305f\u95a2\u6570\u304b\u3089\u5916\u90e8\u306e\u5909\u6570\u306e\u5024\u3092\u5909\u66f4\u3059\u308b<\/h3>\n<p>\u4e0b\u306e\u3088\u3046\u306a\u30b3\u30fc\u30c9\u3067\u306fmethod_b\u5185\u306b\u30ed\u30fc\u30ab\u30eb\u5909\u6570a\u304c\u5b58\u5728\u3057\u3066\u3044\u306a\u3044\u305f\u3081UnboundLocalError\u304c\u8fd4\u3055\u308c\u308b\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef method_a():\r\n    a = 100\r\n    print(a)  # 100\r\n\r\n    def method_b():\r\n        print(a)  # &amp;lt;- NG\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>\u3053\u308c\u3092\u89e3\u6c7a\u3059\u308b\u306b\u306fnonlocal\u6587\u3092\u7528\u3044\u308c\u3070\u826f\u3044\u3053\u3068\u304c\u308f\u304b\u3063\u305f\u3002<\/p>\n<p><a href=\"https:\/\/docs.python.jp\/3\/reference\/simple_stmts.html#grammar-token-nonlocal_stmt\">\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8<\/a>\u3092\u898b\u308b\u3068\u3053\u3046\u66f8\u3044\u3066\u3042\u308b\u3002<\/p>\n<blockquote><p><a class=\"reference internal\" href=\"https:\/\/docs.python.jp\/3\/reference\/simple_stmts.html#nonlocal\"><code class=\"xref std std-keyword docutils literal\"><span class=\"pre\">nonlocal<\/span><\/code><\/a> \u6587\u306f\u3001\u5217\u6319\u3055\u308c\u305f\u8b58\u5225\u5b50\u304c\u30b0\u30ed\u30fc\u30d0\u30eb\u3092\u9664\u304f\u4e00\u3064\u5916\u5074\u306e\u30b9\u30b3\u30fc\u30d7\u3067\u5148\u306b\u675f\u7e1b\u3055\u308c\u305f\u5909\u6570\u3092\u53c2\u7167\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u675f\u7e1b\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u52d5\u4f5c\u304c\u307e\u305a\u30ed\u30fc\u30ab\u30eb\u540d\u524d\u7a7a\u9593\u3092\u63a2\u7d22\u3059\u308b\u306e\u3067\u91cd\u8981\u3067\u3059\u3002\u3053\u306e\u6587\u306f\u3001\u4e2d\u306b\u3042\u308b\u30b3\u30fc\u30c9\u304c\u3001\u30b0\u30ed\u30fc\u30d0\u30eb (\u30e2\u30b8\u30e5\u30fc\u30eb) \u30b9\u30b3\u30fc\u30d7\u4ee5\u5916\u306e\u30ed\u30fc\u30ab\u30eb\u30b9\u30b3\u30fc\u30d7\u306e\u5916\u5074\u306e\u5909\u6570\u3092\u518d\u675f\u7e1b\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>\u30b0\u30ed\u30fc\u30d0\u30eb\u3092\u53c2\u7167\u3057\u305f\u3051\u308c\u3070\u666e\u901a\u306bglobal\u6587\u4f7f\u3048\u3070\u3044\u3044\u3057\u3001\u305d\u308c\u4ee5\u5916\u306e\u4e00\u3064\u5916\u5074\u306e\u30b9\u30b3\u30fc\u30d7\u306e\u5909\u6570\u3092\u53c2\u7167\u3057\u305f\u3051\u308c\u3070nonlocal\u6587\u3092\u4f7f\u3048\u3070\u3044\u3044\u3089\u3057\u3044\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef method_a():\r\n    a = 100\r\n    print(a)  # 100\r\n\r\n    def method_b():\r\n        nonlocal a\r\n        print(a)  # 100 &amp;lt;- OK\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Python\u306e\u30b9\u30b3\u30fc\u30d7\u306b\u95a2\u3057\u3066\u306f\u307e\u3060\u3088\u304f\u308f\u304b\u3089\u306a\u3044\u70b9\u3082\u591a\u3044\u306e\u3067\u8fd1\u3044\u3046\u3061\u306b\u3061\u3083\u3093\u3068\u7406\u89e3\u3057\u3066\u304a\u304d\u305f\u3044&#8230;..<\/p>\n","protected":false},"excerpt":{"rendered":"<p>inner function\u3092\u4f7f\u3046\u3053\u3068\u304c\u306a\u304b\u3063\u305f\u3081\u3061\u3087\u3063\u3068\u8a70\u307e\u3063\u305f\u306e\u3067\u30e1\u30e2\u7a0b\u5ea6\u306b\u3002 Inner Function\u3068\u306f Python\u3067\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u95a2\u6570\u306e\u4e2d\u306b\u66f4\u306b\u95a2\u6570\u3092\u5b9a\u7fa9\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002 def method_a(): &hellip; <a href=\"https:\/\/blog.homi.run\/?p=290\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;Python\u306e\u30cd\u30b9\u30c8\u3055\u308c\u305f\u95a2\u6570\u304b\u3089\u5916\u90e8\u306e\u5909\u6570\u306e\u5024\u3092\u53c2\u7167\u3059\u308b&#8221;\u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-290","post","type-post","status-publish","format-standard","hentry","category-1"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blog.homi.run\/index.php?rest_route=\/wp\/v2\/posts\/290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.homi.run\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.homi.run\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.homi.run\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.homi.run\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=290"}],"version-history":[{"count":9,"href":"https:\/\/blog.homi.run\/index.php?rest_route=\/wp\/v2\/posts\/290\/revisions"}],"predecessor-version":[{"id":300,"href":"https:\/\/blog.homi.run\/index.php?rest_route=\/wp\/v2\/posts\/290\/revisions\/300"}],"wp:attachment":[{"href":"https:\/\/blog.homi.run\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.homi.run\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.homi.run\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}