diff options
| author | nsfisis <nsfisis@gmail.com> | 2022-03-02 20:21:49 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2022-03-02 20:21:49 +0900 |
| commit | 80a0076402d1e6d6bde6c2f9fb78e1eb285ea500 (patch) | |
| tree | b46e0de2f3199d9d2afcc4fd28980ada94d64c31 /docs/posts/python-unbound-local-error/index.html | |
| parent | 0ecfd8dbc7a798d8fa58219d84387b6b30d4a2f4 (diff) | |
| download | nsfisis.github.io-80a0076402d1e6d6bde6c2f9fb78e1eb285ea500.tar.gz nsfisis.github.io-80a0076402d1e6d6bde6c2f9fb78e1eb285ea500.tar.zst nsfisis.github.io-80a0076402d1e6d6bde6c2f9fb78e1eb285ea500.zip | |
enable hasCJKLanguage
Diffstat (limited to 'docs/posts/python-unbound-local-error/index.html')
| -rw-r--r-- | docs/posts/python-unbound-local-error/index.html | 8 |
1 files changed, 1 insertions, 7 deletions
diff --git a/docs/posts/python-unbound-local-error/index.html b/docs/posts/python-unbound-local-error/index.html index cba8819..4012f0e 100644 --- a/docs/posts/python-unbound-local-error/index.html +++ b/docs/posts/python-unbound-local-error/index.html @@ -7,13 +7,7 @@ <title>[Python] クロージャとUnboundLocalError: local variable 'x' referenced before assignment - REPL: Rest-Eat-Program Loop</title> - <meta name="description" content="この記事は Qiita から移植してきたものです。 元 URL: https://qiita.com/nsfisis/items/5d733703afcb35bbf399 - 本記事は Python 3.7.6 の動作結果を元にして書かれている。 -Python でクロージャを作ろうと、次のようなコードを書いた。 -def f(): x = 0 def g(): x += 1 g() f() 関数 g から 関数 f のスコープ内で定義された変数 x を参照し、それに 1 を足そうとしている。 これを実行すると x += 1 の箇所でエラーが発生する。 - UnboundLocalError: local variable ‘x’ referenced before assignment - local変数 x が代入前に参照された、とある。これは、f の x を参照するのではなく、新しく別の変数を g 内に作ってしまっているため。 前述のコードを宣言と代入を便宜上分けて書き直すと次のようになる。var を変数宣言のための構文として擬似的に利用している。 -# 注: var は正しい Python の文法ではない。上記参照のこと def f(): var x # f の local変数 'x' を宣言 x = 0 # x に 0 を代入 def g(): # f の内部関数 g を定義 var x # g の local変数 'x' を宣言 # たまたま f にも同じ名前の変数があるが、それとは別の変数 x += 1 # x に 1 を加算 (x = x + 1 の糖衣構文) # 加算する前の値を参照しようとするが、まだ代入されていないためエラー g() 当初の意図を表現するには、次のように書けばよい。"> + <meta name="description" content="この記事は Qiita から移植してきたものです。 元 URL: https://qiita.com/nsfisis/items/5d733703afcb35bbf399 本記事は Python 3.7.6 の動作結果を元にして書かれている。 Python でクロージャを作ろうと、次のようなコードを書いた"> <meta name="author" content=""> <link href="https://blog.nsfisis.dev/an-old-hope.min.css" rel="stylesheet"> |
