[Python]변수의 범위(Variable Scope)

변수의 범위(Variable Scope)에 대해 알아보자.

"""
변수의 범위 (Variable Scope)
scope, global, nonlocal, locals, globals
"""

# 전역변수
# 지역변수

# 예1
a = 10  # 전역변수


def foo():
    print(f'Ex1 > {a}')


foo()

# 전역변수 출력
print(f'Ex1 > {a}')

'''
Ex1 > 10
Ex1 > 10
'''

# 예2
b = 20  # 전역변수


def bar():
    b = 30  # 로컬변수
    print(f'Ex2 로컬변수 > {b}')


bar()

# 전역변수 출력
print(f'Ex2 전역변수 > {b}')
'''
Ex2 로컬변수 > 30
Ex2 전역변수 > 20
'''

# 예3
c = 40


def foobar():
    # c = c + 10  : 에러발생 (UnboundLocalError: local variable 'c' referenced before assignment)
    print(f'Ex3 > {c}')


foobar()
print(f'Ex3 전역변수 > {c}')
'''
Ex3 > 40
Ex3 전역변수 > 40
'''

# 예4
d = 100


def barfoo():
    global d
    d = d + 10
    print(f'Ex4 > {d}')


barfoo()

print(f'Ex4 전역변수 > {d}')
'''
Ex4 > 110
Ex4 전역변수 > 110
'''


# 예5 (중요)
def outer():
    e = 70   # nonlocal 변수
    def inner():
        nonlocal e
        e += 10
        print(f'Ex5 > {e}')
    return inner


e = outer()  # Closure
e()
e()
'''
Ex5 > 80
Ex5 > 90
'''

# 예6
def func(var):
    x = 10

    def printer():
        print('Ex6 > Printer Func Inner')

    print('Func Inner', locals())

func('Hi')
'''
Func Inner {'var': 'Hi', 'x': 10, 'printer': <function func.<locals>.printer at 0x100afdaf0>}
'''

# 예7
print('Ex7 > ', globals())
test_var = 100
globals()['test_var2'] = 1000
print('Ex7 > ', globals())
'''
Ex7 >  {'__name__': '__main__', '__doc__': '\n변수의 범위 (Variable Scope)\nscope, global, nonlocal, locals, globals\n', '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x104c58cd0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/gabjungim/PycharmProjects/study/level3/py_ad_1_1.py', '__cached__': None, 'a': 10, 'foo': <function foo at 0x104ca5280>, 'b': 20, 'bar': <function bar at 0x104e598b0>, 'c': 40, 'foobar': <function foobar at 0x104e59940>, 'd': 110, 'barfoo': <function barfoo at 0x104e599d0>, 'outer': <function outer at 0x104e59a60>, 'e': <function outer.<locals>.inner at 0x104e59af0>, 'func': <function func at 0x104e59b80>}
Ex7 >  {'__name__': '__main__', '__doc__': '\n변수의 범위 (Variable Scope)\nscope, global, nonlocal, locals, globals\n', '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x104c58cd0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/gabjungim/PycharmProjects/study/level3/py_ad_1_1.py', '__cached__': None, 'a': 10, 'foo': <function foo at 0x104ca5280>, 'b': 20, 'bar': <function bar at 0x104e598b0>, 'c': 40, 'foobar': <function foobar at 0x104e59940>, 'd': 110, 'barfoo': <function barfoo at 0x104e599d0>, 'outer': <function outer at 0x104e59a60>, 'e': <function outer.<locals>.inner at 0x104e59af0>, 'func': <function func at 0x104e59b80>, 'test_var': 100, 'test_var2': 1000}
'''

# 예8
for i in range(1, 11):
    for j in range(1, 11):
        globals()[f'test_{i}_{j}'] = i * j

print(globals())
print('test_1_9 = ', test_1_9)
'''
{'__name__': '__main__', '__doc__': '\n변수의 범위 (Variable Scope)\nscope, global, nonlocal, locals, globals\n', '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x100738cd0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/gabjungim/PycharmProjects/study/level3/py_ad_1_1.py', '__cached__': None, 'a': 10, 'foo': <function foo at 0x100785280>, 'b': 20, 'bar': <function bar at 0x1009398b0>, 'c': 40, 'foobar': <function foobar at 0x100939940>, 'd': 110, 'barfoo': <function barfoo at 0x1009399d0>, 'outer': <function outer at 0x100939a60>, 'e': <function outer.<locals>.inner at 0x100939af0>, 'func': <function func at 0x100939b80>, 'test_var': 100, 'test_var2': 1000, 'i': 10, 'j': 10, 'test_1_1': 1, 'test_1_2': 2, 'test_1_3': 3, 'test_1_4': 4, 'test_1_5': 5, 'test_1_6': 6, 'test_1_7': 7, 'test_1_8': 8, 'test_1_9': 9, 'test_1_10': 10, 'test_2_1': 2, 'test_2_2': 4, 'test_2_3': 6, 'test_2_4': 8, 'test_2_5': 10, 'test_2_6': 12, 'test_2_7': 14, 'test_2_8': 16, 'test_2_9': 18, 'test_2_10': 20, 'test_3_1': 3, 'test_3_2': 6, 'test_3_3': 9, 'test_3_4': 12, 'test_3_5': 15, 'test_3_6': 18, 'test_3_7': 21, 'test_3_8': 24, 'test_3_9': 27, 'test_3_10': 30, 'test_4_1': 4, 'test_4_2': 8, 'test_4_3': 12, 'test_4_4': 16, 'test_4_5': 20, 'test_4_6': 24, 'test_4_7': 28, 'test_4_8': 32, 'test_4_9': 36, 'test_4_10': 40, 'test_5_1': 5, 'test_5_2': 10, 'test_5_3': 15, 'test_5_4': 20, 'test_5_5': 25, 'test_5_6': 30, 'test_5_7': 35, 'test_5_8': 40, 'test_5_9': 45, 'test_5_10': 50, 'test_6_1': 6, 'test_6_2': 12, 'test_6_3': 18, 'test_6_4': 24, 'test_6_5': 30, 'test_6_6': 36, 'test_6_7': 42, 'test_6_8': 48, 'test_6_9': 54, 'test_6_10': 60, 'test_7_1': 7, 'test_7_2': 14, 'test_7_3': 21, 'test_7_4': 28, 'test_7_5': 35, 'test_7_6': 42, 'test_7_7': 49, 'test_7_8': 56, 'test_7_9': 63, 'test_7_10': 70, 'test_8_1': 8, 'test_8_2': 16, 'test_8_3': 24, 'test_8_4': 32, 'test_8_5': 40, 'test_8_6': 48, 'test_8_7': 56, 'test_8_8': 64, 'test_8_9': 72, 'test_8_10': 80, 'test_9_1': 9, 'test_9_2': 18, 'test_9_3': 27, 'test_9_4': 36, 'test_9_5': 45, 'test_9_6': 54, 'test_9_7': 63, 'test_9_8': 72, 'test_9_9': 81, 'test_9_10': 90, 'test_10_1': 10, 'test_10_2': 20, 'test_10_3': 30, 'test_10_4': 40, 'test_10_5': 50, 'test_10_6': 60, 'test_10_7': 70, 'test_10_8': 80, 'test_10_9': 90, 'test_10_10': 100}
test_1_9 =  9
'''

You may also like...

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다