4.10 네임스페이스와 스코프

** 네임스페이스는 특정 이름이 유일하고, 다른 네임스페이스에서의 같은 이름과 관계가 없는 것을 말한다. **

animal = 'fruitbat'
def print_global():
print('inside print_global:', animal)
print('at the top level:', animal)

at the top level: fruitbat

print_global()

inside print_global: fruitbat

함수에서 전역 변수 읽기 가능

def change_and_global():
print('inside change_and_print global:', animal)
animal = 'wombat'
print('after the change:', animal)
change_and_global()
UnboundLocalError Traceback (most recent call last)
<ipython-input-6-b29afb4d5663> in <module>()
3     animal = 'wombat'
4     print('after the change:', animal)
----> 5 change_and_global()

<ipython-input-6-b29afb4d5663> in change_and_global()
1 def change_and_global():
----> 2     print('inside change_and_print global:', animal)
3     animal = 'wombat'
4     print('after the change:', animal)
5 change_and_global()
UnboundLocalError: local variable 'animal' referenced before assignment

함수에서 전역 변수를 바꾸려 하면 또다른 같은 이름의 변수를 변경하려 함 그래서 에러가 남.

def change_local():
animal = 'wombat'
print('inside change_local:', animal, id(animal))
change_local()

inside change_local: wombat 140395106534264

animal

‘fruitbat’

id(animal)

140395115337264

animal = 'fruibat'
def change_and_print_global():
global animal
animal = 'wombat'
print('inside change_and_print_global:', animal)
animal

‘fruibat’

change_and_print_global()

inside change_and_print_global: wombat

animal

‘wombat’ 함수내에 global 키워드를 통해 전역 변수의 접근을 허용 시킨다. ** 파이썬은 네임 스페이스의 내용을 접근하기 위해 두 가지 함수를 제공한다. **

  • locals() 함수는 로컨 네임스페이스의 내용이 담긴 딕셔너리를 반환한다.
  • globals() 함수는 글로벌 네임스페이스의 내용이 담긴 딕셔너리를 반환한다.
    animal = 'fruitbat'
    def change_local():
    animal = 'wombat' # 지역 변수
    print('plocals:', locals())
    
    print(animal)
    change_local()
    

    fruitbat plocals: {‘animal’: ‘wombat’}

    print('globals:', globals())
    
globals: {'__name__': '__main__', '__doc__': 'Automatically created module for IPython interactive environment', '__package__': None, '__loader__': None, '__spec__': None, '__builtin__': <module 'builtins' (built-in)>, '__builtins__': <module 'builtins' (built-in)>, '_ih': ['', "animal = 'fruitbat'\ndef print_global():\n    print('inside print_global:', animal)", "animal = 'fruitbat'\ndef print_global():\n    print('inside print_global:', animal)\n\n    print('at the top level:', animal)", "animal = 'fruitbat'\ndef print_global():\n    print('inside print_global:', animal)\n\nprint('at the top level:', animal)", 'print_global()', "def change_and_global():\n    print('inside change_and_print global:', animal)\n    animal = 'wombat'\n    print('after the change:', animal)\n    ", "def change_and_global():\n    print('inside change_and_print global:', animal)\n    animal = 'wombat'\n    print('after the change:', animal)\nchange_and_global()", "def change_local():\n    animal = 'wombat'\n    print('inside change_local:', animal, id(animal))", "def change_local():\n    animal = 'wombat'\n    print('inside change_local:', animal, id(animal))\nchange_local()", 'animal', 'id(animal)', "animal = 'fruibat'\ndef change_and_print_global():\n    global animal\n    animal = 'wombat'\n    print('inside change_and_print_global:', animal)", 'animal', 'change_and_print_global()', 'animal', "animal = 'fruitbat'\ndef change_local():\n    animal = 'wombat' # 지역 변수\n    print('plocals:', locals())", 'print(animal)\nchange_lolcal()', 'print(animal)\nchange_lolcal()', "animal = 'fruitbat'\ndef change_local():\n    animal = 'wombat' # 지역 변수\n    print('plocals:', locals())", 'print(animal)\nchange_local()', "print('globals:', globals()) "], '_oh': {9: 'fruitbat', 10: 140395115337264, 12: 'fruibat', 14: 'wombat'}, '_dh': ['/home/sumin/projects/python/IntroducingPython/4. 파이 크러스트: 코드 구조'], 'In': ['', "animal = 'fruitbat'\ndef print_global():\n    print('inside print_global:', animal)", "animal = 'fruitbat'\ndef print_global():\n    print('inside print_global:', animal)\n\n    print('at the top level:', animal)", "animal = 'fruitbat'\ndef print_global():\n    print('inside print_global:', animal)\n\nprint('at the top level:', animal)", 'print_global()', "def change_and_global():\n    print('inside change_and_print global:', animal)\n    animal = 'wombat'\n    print('after the change:', animal)\n    ", "def change_and_global():\n    print('inside change_and_print global:', animal)\n    animal = 'wombat'\n    print('after the change:', animal)\nchange_and_global()", "def change_local():\n    animal = 'wombat'\n    print('inside change_local:', animal, id(animal))", "def change_local():\n    animal = 'wombat'\n    print('inside change_local:', animal, id(animal))\nchange_local()", 'animal', 'id(animal)', "animal = 'fruibat'\ndef change_and_print_global():\n    global animal\n    animal = 'wombat'\n    print('inside change_and_print_global:', animal)", 'animal', 'change_and_print_global()', 'animal', "animal = 'fruitbat'\ndef change_local():\n    animal = 'wombat' # 지역 변수\n    print('plocals:', locals())", 'print(animal)\nchange_lolcal()', 'print(animal)\nchange_lolcal()', "animal = 'fruitbat'\ndef change_local():\n    animal = 'wombat' # 지역 변수\n    print('plocals:', locals())", 'print(animal)\nchange_local()', "print('globals:', globals()) "], 'Out': {9: 'fruitbat', 10: 140395115337264, 12: 'fruibat', 14: 'wombat'}, 'get_ipython': <bound method InteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x7fb04c8d73c8>>, 'exit': <IPython.core.autocall.ZMQExitAutocall object at 0x7fb049850400>, 'quit': <IPython.core.autocall.ZMQExitAutocall object at 0x7fb049850400>, '_': 'wombat', '__': 'fruibat', '___': 140395115337264, '_i': 'print(animal)\nchange_local()', '_ii': "animal = 'fruitbat'\ndef change_local():\n    animal = 'wombat' # 지역 변수\n    print('plocals:', locals())", '_iii': 'print(animal)\nchange_lolcal()', '_i1': "animal = 'fruitbat'\ndef print_global():\n    print('inside print_global:', animal)", 'animal': 'fruitbat', 'print_global': <function print_global at 0x7fb048733510>, '_i2': "animal = 'fruitbat'\ndef print_global():\n    print('inside print_global:', animal)\n\n    print('at the top level:', animal)", '_i3': "animal = 'fruitbat'\ndef print_global():\n    print('inside print_global:', animal)\n\nprint('at the top level:', animal)", '_i4': 'print_global()', '_i5': "def change_and_global():\n    print('inside change_and_print global:', animal)\n    animal = 'wombat'\n    print('after the change:', animal)\n    ", 'change_and_global': <function change_and_global at 0x7fb048733d08>, '_i6': "def change_and_global():\n    print('inside change_and_print global:', animal)\n    animal = 'wombat'\n    print('after the change:', animal)\nchange_and_global()", '_i7': "def change_local():\n    animal = 'wombat'\n    print('inside change_local:', animal, id(animal))", 'change_local': <function change_local at 0x7fb04864fd08>, '_i8': "def change_local():\n    animal = 'wombat'\n    print('inside change_local:', animal, id(animal))\nchange_local()", '_i9': 'animal', '_9': 'fruitbat', '_i10': 'id(animal)', '_10': 140395115337264, '_i11': "animal = 'fruibat'\ndef change_and_print_global():\n    global animal\n    animal = 'wombat'\n    print('inside change_and_print_global:', animal)", 'change_and_print_global': <function change_and_print_global at 0x7fb04864f950>, '_i12': 'animal', '_12': 'fruibat', '_i13': 'change_and_print_global()', '_i14': 'animal', '_14': 'wombat', '_i15': "animal = 'fruitbat'\ndef change_local():\n    animal = 'wombat' # 지역 변수\n    print('plocals:', locals())", '_i16': 'print(animal)\nchange_lolcal()', '_i17': 'print(animal)\nchange_lolcal()', '_i18': "animal = 'fruitbat'\ndef change_local():\n    animal = 'wombat' # 지역 변수\n    print('plocals:', locals())", '_i19': 'print(animal)\nchange_local()', '_i20': "print('globals:', globals()) "}
animal

‘fruitbat’

4.10.1 이름에 _와 __사용

** 파이썬 내의 사용을 위해 예약 되어 있다. 시스템 변수 function.name 이나 fucntion__doc__등이 있다. **

def amazing():
'''This is the amazing function.
Want to see it again?'''
print('THis function is named:', amazing.__name__)
print('And its docsting is:', amazing.__doc__)
amazing()

THis function is named: amazing And its docsting is: This is the amazing function. Want to see it again?

Comments