위키백과에 따르면 프로그래밍 용어 중 자유 변수 free variable 의 의미는 아래와 같다.
In computer programming, the term free variable refers to variables used in a function that are neither local variables nor parameters of that function. The term non-local variable is often a synonym in this context.
파이썬 문서에서 자유 변수의 정의는 아래와 같다.
If a variable is used in a code block but not defined there, it is a free variable.
그러니까 정확하지는 않지만 지역 변수가 아니면 자유 변수인 것 같다.
예를 들어 아래 코드에서 변수 free
는 함수 inside
의 입장에서는 지역 변수가 아니므로 자유 변수이다.
def outside():
free = 1
def inside():
print(free)
자유 변수를 알면 클로저나 데코레이터를 다루는데 도움이 될 수 있다.