데코레이터 미사용 샘플 문의 - 결과가 2개씩 나옵니다.
해결됨
우리를 위한 프로그래밍 : 파이썬 중급 (Inflearn Original)
강사님 질문 있습니다. 아래처럼 코드를 사용해서 실행하니 결과가 2개씩 나오는데, 이유가 무엇인가요? # 데코레이터 실습 import time def perf_clock ( func ) : def perf_clocked ( * args ) : # 함수 시작 시간 st = time.perf_counter () result = func ( * args ) # 함수 종료 시간 계산 et = time.perf_counter () - st # 실행 함수명 name = func.__name__ # 함수 매개변수 arg_str = ' , ' . join ( repr ( arg ) for arg in args ) # 결과 출력 print ( ' [ %0.5f s] %s ( %s ) -> %r ' % (et, name, arg_str, result)) return result return perf_clocked @ perf_clock def time_func ( seconds ) : time.sleep ( seconds ) @ perf_clock def sum_func ( * numbers ) : return sum ( numbers ) # 데코레이터 미사용 none_deco1 = perf_clock ( time_func ) none_deco2 = perf_clock ( sum_func ) # print(none_deco1, none_deco1.__code__.co_freevars) # print(none_deco2, none_deco2.__code__.co_freevars) print () print ( ' - ' * 40 , ' Called None Decorator -> time_func ' ) none_deco1 ( 1.5 ) print () print ( ' - ' * 40 , ' Called None Decorator -> sum_func ' ) none_deco2 ( 100 , 150 , 250 , 300 , 350 ) 결과는 아래와 같습니다. (env_craw) D:\workspace\Python>python decolate.py ---------------------------------------- Called None Decorator -> time_func [1.51387s] time_func(1.5) -> None [1.51407s] perf_clocked(1.5) -> None ---------------------------------------- Called None Decorator -> sum_func [0.00001s] sum_func(100, 150, 250, 300, 350) -> 1150 [0.00014s] perf_clocked(100, 150, 250, 300, 350) -> 1150
- 데코레이터
- 데코레이터미사용
- django
- python





