CHAAANY ARCHIVE

functools wraps

1개의 기록을 주제별로 둘러보세요.

Python 클로저와 데코레이터: late binding·nonlocal·wraps까지

Python closure는 바깥 함수의 local name을 안쪽 함수가 기억하는 구조다. 이를 이용하면 작은 상태를 함수와 함께 묶고 factory function이나 decorator를 만들 수 있다. 다만 closure를 곧바로 ‘private data’나 ‘보안상 은닉’이라고 부르면 범위를 과장하게 된다. Python에서는 introspection이 가능하고 access control을 강제하는 장치가 아니기 때문이다.closure가 기억하는 것은 lexical scope의 binding이다다음 counter는 make_counter() 실행이 끝난 뒤에도 count binding을 유지한다.def make_counter(start=0): count = start def increme..

728x90