Notice
Recent Posts
Recent Comments
Link
목록함수내변수수정 (1)
SeouliteLab
[Python/파이썬] nonlocal과 global 사용 방법
nonlocal과 global은 파이썬에서 변수의 범위(scope)를 지정하는 데 사용되는 키워드입니다. nonlocal은 중첩 함수에서 외부 함수의 지역 변수를 수정할 때 사용되며, global은 전역 변수를 함수 내에서 수정할 때 사용됩니다. 이들 키워드의 사용법과 예제를 살펴보겠습니다. nonlocal 키워드 사용 방법 def outer_function(): x = 10 def inner_function(): nonlocal x x = 20 inner_function() print("inner_function 실행 후 x:", x) outer_function() 위 예제에서는 inner_function 내부에서 outer_function의 변수 x를 nonlocal로 선언하여 변수 x를 수정하고 있..
프로그래밍
2024. 2. 29. 17:19