summaryrefslogtreecommitdiffhomepage
path: root/vhosts/blog/public/posts/2021-10-02/python-unbound-local-error
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2023-09-20 19:56:52 +0900
committernsfisis <nsfisis@gmail.com>2023-09-20 19:56:57 +0900
commita84908b7e8a0e2423afd6b836eccf27a420270b4 (patch)
tree00204b62358f8c57fcb36f601db360626484cc1a /vhosts/blog/public/posts/2021-10-02/python-unbound-local-error
parent0b488f85380f964c40b0b9aae69c6611bc7978bc (diff)
downloadnsfisis.dev-a84908b7e8a0e2423afd6b836eccf27a420270b4.tar.gz
nsfisis.dev-a84908b7e8a0e2423afd6b836eccf27a420270b4.tar.zst
nsfisis.dev-a84908b7e8a0e2423afd6b836eccf27a420270b4.zip
feat(blog/nuldoc): change content format from DocBook to NulDoc
Diffstat (limited to 'vhosts/blog/public/posts/2021-10-02/python-unbound-local-error')
-rw-r--r--vhosts/blog/public/posts/2021-10-02/python-unbound-local-error/index.html6
1 files changed, 3 insertions, 3 deletions
diff --git a/vhosts/blog/public/posts/2021-10-02/python-unbound-local-error/index.html b/vhosts/blog/public/posts/2021-10-02/python-unbound-local-error/index.html
index 105b946b..cb6efafb 100644
--- a/vhosts/blog/public/posts/2021-10-02/python-unbound-local-error/index.html
+++ b/vhosts/blog/public/posts/2021-10-02/python-unbound-local-error/index.html
@@ -73,7 +73,7 @@
Python でクロージャを作ろうと、次のようなコードを書いた。
</p>
- <pre class="highlight" language="python" linenumbering="unnumbered"><code class="highlight"><span class="hljs-keyword">def</span> <span class="hljs-title function_">f</span>():
+ <pre class="highlight" language="python"><code class="highlight"><span class="hljs-keyword">def</span> <span class="hljs-title function_">f</span>():
x = <span class="hljs-number">0</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">g</span>():
x += <span class="hljs-number">1</span>
@@ -95,7 +95,7 @@ f()</code></pre>
local変数 <code>x</code> が代入前に参照された、とある。これは、<code>f</code> の <code>x</code> を参照するのではなく、新しく別の変数を <code>g</code> 内に作ってしまっているため。 前述のコードを宣言と代入を便宜上分けて書き直すと次のようになる。<code>var</code> を変数宣言のための構文として擬似的に利用している。
</p>
- <pre class="highlight" language="python" linenumbering="unnumbered"><code class="highlight"><span class="hljs-comment"># 注: var は正しい Python の文法ではない。上記参照のこと</span>
+ <pre class="highlight" language="python"><code class="highlight"><span class="hljs-comment"># 注: var は正しい Python の文法ではない。上記参照のこと</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">f</span>():
var x <span class="hljs-comment"># f の local変数 &#x27;x&#x27; を宣言</span>
x = <span class="hljs-number">0</span> <span class="hljs-comment"># x に 0 を代入</span>
@@ -110,7 +110,7 @@ f()</code></pre>
当初の意図を表現するには、次のように書けばよい。
</p>
- <pre class="highlight" language="python" linenumbering="unnumbered"><code class="highlight"><span class="hljs-keyword">def</span> <span class="hljs-title function_">f</span>():
+ <pre class="highlight" language="python"><code class="highlight"><span class="hljs-keyword">def</span> <span class="hljs-title function_">f</span>():
x = <span class="hljs-number">0</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">g</span>():
<span class="hljs-keyword">nonlocal</span> x <span class="hljs-comment">## (*)</span>