Skip to Content
전문가를 위한 파이썬(2판)
book

전문가를 위한 파이썬(2판)

by 루시아누 하말류, 강권학
November 2024
Beginner to intermediate
1064 pages
25h 21m
Korean
Hanbit Media, Inc.
Content preview from 전문가를 위한 파이썬(2판)
381
9
데커레이터와 클로저
예제
9-10
[예제
9
-
8
]의
make
_
averager
()
로 생성한 함수 조사하기
>>> avg.__code__.co_varnames
(‘new_value’, ‘total’)
>>> avg.__code__.co_freevars
(‘series’,)
series
의 값은 반환된
avg( )
함수의
__closure__
속성에 저장된다.
avg.__closure__
각 항목은
avg.__code__.co_freevars
의 이름에 대응된다. 이 항목들은
cell
객체이며, 이
객체의
cell_contents
속성에서 실젯값을 찾을 수 있다. [예제
9
-
11
]은 이 속성들을 보여 준다.
예제
9-11
[예제
9
-
9
]에서 이어짐
>>> avg.__code__.co_freevars
(‘series’,)
>>> avg.__closure__
(<cell at 0x107a44f78: list object at 0x107a91a48>,)
>>> avg.__closure__[0].cell_contents
[10, 11, 12]
지금까지 설명한 내용을 정리해 보자. 클로저는 함수가 정의될 때 존재하던 자유 변수에 대한
바인딩을 유지하는 함수이다. 따라서 함수를 정의하는 범위가 사라진 후 함수를 호출해도 자유
변수에 접근할 수 있다.
함수가 비전역 외부 변수를 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

전문가를 위한 리액트

전문가를 위한 리액트

테자스 쿠마르
고성능 파이썬(2판)

고성능 파이썬(2판)

오현석, 미샤 고렐릭, 이안 오스발트

Publisher Resources

ISBN: 9791169211772