kioto
December 09, 2020
630

# Pythonのグローバル変数について

Start Python Club #64 LT

## kioto

December 09, 2020

## Transcript

4. ### w ϞδϡʔϧείʔϓͰ୅ೖ͞Εͨม਺ w ؔ਺಺ͰࢀরՄೳ άϩʔόϧม਺ͱ͸Կ͔ # -*- coding:utf-8 -*- x

= 0 # άϩʔόϧม਺ def foo(): print(x) foo() ˞1ZUIPO+BQBOθϩ͔Βͷ1ZUIPOೖ໳ߨ࠲ ʮϩʔΧϧม਺ͱάϩʔόϧม਺ʯΑΓ IUUQTXXXQZUIPOKQUSBJOGVODUJPOMPDBM@WBSIUNM
5. ### w ؔ਺಺Ͱ୅ೖͰ͖ͳ͍ w ͦͷ··ͩͱϩʔΧϧม਺ʹͳΔ άϩʔόϧม਺ͱ͸Կ͔ # -*- coding:utf-8 -*- x

= 0 # άϩʔόϧม਺ def bar(): x = 999 print(x) def foo(): print(x) bar() # => 999 foo() # => 0
6. ### ؔ਺಺Ͱάϩʔόϧม਺Λ୅ೖ w HMPCBMจΛ࢖͏ # -*- coding:utf-8 -*- x = 0

def bar(): global x x = 10 def foo(): print(x) bar() foo() # => 10
7. ### HMPCBMจͷఆٛ w ެࣜυΩϡϝϯτΑΓ w  HMPCBMจ͸ɺݱࡏͷίʔυϒϩοΫશମͰҡ࣋͞ΕΔએݴจͰ͢ɻ w  HMPCBMจ͸ɺྻڍͨࣝ͠ผࢠΛάϩʔόϧม਺ͱͯ͠ղऍ͢ΔΑ͏ࢦ ఆ͢Δ͜ͱΛҙຯ͠·͢ɻ

IUUQTEPDTQZUIPOPSHKBSFGFSFODFTJNQMF@TUNUTIUNM IJHIMJHIUHMPCBMUIFHMPCBMTUBUFNFOU
8. ### HMPCBMจͷఆٛ # -*- coding:utf-8 -*- x = 0 def bar():

global x x = 10 def foo(): print(x) bar() foo() # => 10  HMPCBMจ͸ɺݱࡏͷίʔυϒϩο ΫશମͰҡ࣋͞ΕΔએݴจͰ͢ɻ  ݱࡏͷίʔυϒϩοΫ w  HMPCBMจ͸ɺྻڍͨࣝ͠ผࢠΛ άϩʔόϧม਺ͱͯ͠ղऍ͢ΔΑ͏ࢦ ఆ͢Δ͜ͱΛҙຯ͠·͢ɻ  ྻڍͨࣝ͠ผࢠ
9. ### ʢඇਪ঑ʣϞδϡʔϧείʔϓͰະఆٛ w ϞδϡʔϧείʔϓͰະఆٛͰ΋ಈ͘ w Մಡੑ͕ѱ͍ ˠάϩʔόϧม਺Yͷॳظ஋͸ʁ ɹؔ਺ͷ࣮ߦॱংͷ֬ೝ͕ඞཁʹͳΔ # -*- coding:utf-8

-*- # x = 0 def bar(): global x x = 10 def foo(): print(x) bar() foo() # => 10
10. ### ʢඇਪ঑ʣϞδϡʔϧείʔϓͰະఆٛ w ౰વɺઌʹࢀর͢ΔͱΤϥʔ # -*- coding:utf-8 -*- # x =

0 def bar(): global x x = 10 def foo(): print(x) foo() # ERROR! bar()
11. ### ʢඇਪ঑ʣࢀর͚ͩͳͷʹHMPCBMจ w Մಡੑ͕ѱ͘ͳΔ w ୅ೖ͢Δͭ΋Γͩͬͨʁόάʁ # -*- coding:utf-8 -*- x

= 0 def bar(): global x x = 10 # xʹ୅ೖ͍ͯ͠Δ def foo(): global x print(x) # x͸୅ೖ͠ͳ͍ͷʁ bar() foo() # => 10
12. ### ϩʔΧϧม਺ͷ༏ઌ w άϩʔόϧม਺ͱಉ໊ͷϩʔΧϧม਺͸ɺ ϩʔΧϧม਺͕༏ઌ w ʢͦ΋ͦ΋άϩʔόϧม਺YͬͯϩʔΧϧ ม਺ͱݟ෼͚͕͖ͭʹ͍͘ʂʣ # -*- coding:utf-8

-*- x = 0 def foo(): x = 10 # x͸ϩʔΧϧม਺ print(x) foo() # => 10
13. ### 1&1ͷάϩʔόϧม਺ͷ໋໊ϧʔϧ w ϞδϡʔϧείʔϓͰඇެ։ͳΒ͹ɺઌ಄ ʹΞϯμʔείΞΛೖΕΔ w ࢀরൣғ͕޿͍ม਺͸ɺԿͷ஋͔෼͔Δ Α͏ʹɺͪΌΜͱҙຯͷ͋Δ໊લʹ͢Δ ͜ͱ # -*-

coding:utf-8 -*- _value = 0 def foo(): global _value _value = 10 y = 20 print(_value, y) foo() # => 10, 20
14. ### HMPCBMจͷҐஔ w HMPCBMจ͸ؔ਺ͷઌ಄Ͱͳͯ͘΋ಈ͘ w ͨͩ͠ɺಡΈʹ͍͘ͷͰHMPCBMจ͸ؔ਺ ͷઌ಄͕͍͍ʢݸਓͷײ૝ʣ # -*- coding:utf-8 -*-

_value = 0 def foo(): y = 20 global _value # OK _value = 10 print(_value, y) foo() # => 10,20

16. ### ଞͷϞδϡʔϧείʔϓͷม਺ࢀর w Α༷ͦͷάϩʔόϧม਺Λ࢖͏ # -*- coding:utf-8 -*- import mymod def

print_val(): print(mymod.val) def set_val(num): mymod.val = num print_val() # => 0 set_val(456) print_val() # => 456 SVOQZ # -*- coding:utf-8 -*- val = 0 NZNPEQZ
17. ### ଞͷϞδϡʔϧείʔϓͷม਺ࢀর w Α༷ͦͷάϩʔόϧม਺Λɺࣗ෼ͷͱ͜Ζ ͷάϩʔόϧม਺ͱͯ͠࢖͏ # -*- coding:utf-8 -*- from mymod

import * def print_val(): print(val) def set_val(num): global val val = num print_val() # => 0 set_val(456) print_val() # => 456 SVOQZ # -*- coding:utf-8 -*- val = 0 NZNPEQZ
18. ### ଞͷϞδϡʔϧείʔϓͷม਺ࢀর w ͞Βʹࣗ෼ͷͱ͜ΖͰάϩʔόϧม਺Λ࡞ ΔͱɺNZNPEWBM͕ݟ͑ͳ͘ͳΔ w ͭ·Γ͜͏͍͏ॻ͖ํΛ͢΂͖Ͱͳ͍ # -*- coding:utf-8 -*-

from mymod import * val = 999 def print_val(): print(val) def set_val(num): global val val = num print_val() # => 999 set_val(456) print_val() # => 456 SVOQZ # -*- coding:utf-8 -*- val = 0 NZNPEQZ

20. ### άϩʔόϧม਺ͷࢀর w ̎֊૚ͷؔ਺ఆٛͷྫ # -*- coding:utf-8 -*- x = 0

def func01(): x = 99 def func02(): # global x print('func02', x) func02() print('func01', x) func01() => func02 99 => func01 99 SFTVMU
21. ### άϩʔόϧม਺ͷࢀর w ̎֊૚ͷؔ਺ఆٛͷྫ ʢHMPCBMจ௥Ճʣ # -*- coding:utf-8 -*- x =

0 def func01(): x = 99 def func02(): global x print('func02', x) func02() print('func01', x) func01() => func02 0 => func01 99 SFTVMU
22. ### ༨ஊ w ಉ໊ͷϩʔΧϧม਺ͱάϩʔόϧม਺͸ɺಉ͡είʔϓͰ࢖͑ͳ͍ \$ python3 sample.py File "sample.py", line 8

global x ^ SyntaxError: name 'x' is used prior to global declaration # -*- coding:utf-8 -*- x = 0 def foo(): x = 10 print(x) global x x = 20 print(x) foo() SFTVMU