본문 바로가기
목차
Python

[Ex] scope 이해.

by ds31x 2025. 5. 12.
728x90
반응형

다음 코드의 동작을 주석을 달아서 설명해보자.

x = 10

def func_one():
    x = 20

    def func_two():
        y = 30

        def func_three():
            y = 11
            nonlocal x
            print(f"func_three: {x = }, {y = }")
            x = 777
        
        func_three()
        print(f"func_two: {x = }, {y = }")
    
    func_two()
    print(f"func_one: {x = }")


func_one()
print(f"global: {x = }")

 

참고:

2023.07.15 - [Python] - [Python] scope와 키워드 global, nonlocal

 

[Python] scope와 키워드 global, nonlocal

Python에서 scope는namespace와 밀접하게 관련이 있는 개념이며,이와 관련된 주요 키워드가 nonlocal과 global이 있음.https://dsaint31.tistory.com/entry/Basic-namespace-frame-and-context [Basic] namespace, frame, and contextNamespa

ds31x.tistory.com

 

728x90

'Python' 카테고리의 다른 글

[Py] subprocess 모듈 사용법.  (1) 2025.06.07
[Py] SymPy (Symbolic Python) - Symbolic Computation  (1) 2025.06.05
[PySide] QtCore.QSettings 사용법  (0) 2025.05.12
[Programming] SOLID 원칙  (0) 2025.04.28
[DL] default collate_fn - PyTorch  (0) 2025.04.26