0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

1.スコープ

Pythonのスコープについてのメモ

同じ名前の変数valをグローバル変数とローカル変数として作って動きを確認

val = 100        # グローバル変数
def showVal():
    val = 1      # ローカル変数
    print("local val : ",val)
    
showVal()
print("global val : ",val)
実行結果
local val :  1
global val :  100

関数内でグローバル変数に値を代入

val = 100        # グローバル変数
def showVal():
    global val      # 変数がグローバルである事を明記
    val = 1 
    print("local val : ",val)
    
showVal()
print("global val : ",val)
実行結果
local val :  1
global val :  1
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?