Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

F907572635730576b6d609dc63ca4f52?s=47 kioto
December 09, 2020

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

Start Python Club #64 LT

F907572635730576b6d609dc63ca4f52?s=128

kioto

December 09, 2020
Tweet

Transcript

  1. ̕,JPUP)JSBIBSB 1ZUIPOͷ άϩʔόϧม਺ʹ͍ͭͯ 4UBSU1ZUIPO$MVC-5

  2. ͓લɺ୭Α w ฏݪوԻʢͻΒ͸Β͖͓ͱʣ w ࡾӜ൒ౡͰϑϦʔϥϯεͷϓϩάϥϚͯ͠·͢ w 4UBSU1ZUIPO$MVC໊ݹ԰ࢧ෦ελοϑ w 1ZUIPO#PPU$BNQJOח૔ͷݱ஍ελοϑʢ։࠵༧ఆʣ

  3. 1ZUIPO#PPU$BNQJOח૔ɹ։࠵͠·͢ IUUQTQZDPOKQDPOOQBTTDPNFWFOU ೥݄೔ʢ౔ʣʙ )"546ח૔ʹͯ։࠵

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

    = 0 # άϩʔόϧม਺ def foo(): print(x) foo() ˞1ZUIPO+BQBOθϩ͔Βͷ1ZUIPOೖ໳ߨ࠲ ʮϩʔΧϧม਺ͱάϩʔόϧม਺ʯΑΓ IUUQTXXXQZUIPOKQUSBJOGVODUJPOMPDBM@WBSIUNM
  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จ͸ɺྻڍͨࣝ͠ผࢠΛάϩʔόϧม਺ͱͯ͠ղऍ͢ΔΑ͏ࢦ ఆ͢Δ͜ͱΛҙຯ͠·͢ɻ

    IUUQTEPDTQZUIPOPSHKBSFGFSFODFTJNQMF@TUNUTIUNM IJHIMJHIUHMPCBMUIFHMPCBMTUBUFNFOU
  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
  15. ༨ஊ w ʮάϩʔόϧʯ࣮ͬͯ͸ਖ਼֬Ͱ͸ͳ͍ w ຊདྷɺͦͷ··Ͱ͸Ϟδϡʔϧείʔϓ֎͔ΒಁաͰͳ͍͔Β w ศ্ٓɺʮϞδϡʔϧείʔϓʯͷ͜ͱΛʮάϩʔόϧʯͱݺΜͰ͍Δ w 1ZUIPOͷެࣜจॻͰ΋ʮάϩʔόϧม਺ʯ͸࢖ΘΕ͍ͯΔ

  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 SVOQZ # -*- coding:utf-8 -*- val = 0 NZNPEQZ
  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 SVOQZ # -*- coding:utf-8 -*- val = 0 NZNPEQZ
  18. ଞͷϞδϡʔϧείʔϓͷม਺ࢀর w ͞Βʹࣗ෼ͷͱ͜ΖͰάϩʔόϧม਺Λ࡞ ΔͱɺNZNPEWBM͕ݟ͑ͳ͘ͳΔ 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 SVOQZ # -*- coding:utf-8 -*- val = 0 NZNPEQZ
  19. ༨ஊ̎ w ̎֊૚Ҏ্ͷ֊૚ͰɺHMPCBMจ͸ࢀরʹ΋࢖͑Δ w ͦ΋ͦ΋ϩʔΧϧม਺ͱಉ໊͡લͷάϩʔόϧม਺໊͸ɺͲ͏͔ͱࢥ͏ Αɺͱ͍͏͜ͱͰඇਪ঑Ͱ͢ w ໘౗ͳͷͰɺOPOMPDBMจͷ࿩͸͠·ͤΜ

  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