[Python] 클로저(Closure)와 내부 함수를 알아보자


[Python] 클로저(Closure)와 내부 함수를 알아보자

클로저(Closure) 내부 함수 내부 함수란, 함수 안에서 정의되는 함수이다 예제를 통해 살펴보도록 하겠다. def outer(name): def inner(): print("Hello ", name) return inner func = outer("Lee") func() # >>> Hello Lee예제를 실행시키면, 먼저 func = outer("Lee")에서 outer함수가 실행된다. 이후, outer함수의 parameter인 name에 "Lee"이 할당되게 된다. 이 때, name은 외부 함수 outer에서 사용하는 지역 변수이다. 그 다음 내부 함수 inner가 정의되고, 이 내부 함수 inner를 반환하게 된다. 그렇다면, func = outer("Lee") 표현식에서 변수 func에는 어떤 ..


원문링크 : [Python] 클로저(Closure)와 내부 함수를 알아보자