728x90
반응형
decorator 패턴은 '@'표기를 사용해 함수 또는 메서드의 변환을 우아하게 지정해준다
'함수의 객체'와 '함수를 변경하는 다른 객체'의 wrapping을 허용한다
@deco
def method(arg):
# method...
pass
decorator 를 사용한 위 코드는 아래코드와 같다
def method(arg):
# method...
pass
method = deco(method)
import functools
import time
def print_time_func(func):
@functools.wraps(func)
def wrapper(*args, **kargs):
st = time.time()
res = func(*args, **kargs)
print(f'{func.__name__}: {time.time()-st:.4f}')
return res
return wrapper
@print_time_func
def mothod():
# method...
print('[method]')
pass
>>> method()
[method]
method: 0.0000
728x90
728x90
BIG
'Programming > Python' 카테고리의 다른 글
list method 성능 측정 (0) | 2020.09.15 |
---|---|
Index(), bisect() - list 원소의 index값 찾기 (0) | 2020.09.14 |
named tuple - 네임드듀플 (0) | 2020.09.11 |
permutations in itertools (0) | 2020.09.04 |
list[::], tuple[::] - extended slices (0) | 2020.09.02 |
댓글