함수의 첫 번째 인수로 넘겨준다. 그러면 다른 인수를 받는 데커레이터를 어떻게 만들까? 인수
를 받아 데커레이터를 반환하는 데커레이터 팩토리를 만들고 나서, 데커레이트될 함수에 데커
레이터 팩토리를 적용하면 된다. 설명이 복잡한가? 아마 그럴 것이다. 지금까지 우리가 본 가
장 간단한 데커레이터인 [예제
9
-
21
]의
register()
를 예로 들어 알아보자.
예제
9-21
[예제
9
-
2
]의
registration
.
py
모듈의 축약 버전
registry = []
def register(func):
print(f’running register({func})’)
registry.append(func)
return func
@register
def f1():
print(‘running f1()’)
print(‘running main()’)
print(‘registry ->’, registry)
f1()
9
.
10
.
1
매개변수화된 등록 데커레이터
register()
가 등록하는 함수를 활성화하거나 비활성화하기 쉽도록, 선택적인 인수
active
를 받게 해 보자.
active
가
False
면 데커레이트된 함수를 등록 해제한다. [예제
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.