import datetime
def datetime_decorator(func):
def decorated():
print datetime.datetime.now()
func()
print datetime.datetime.now()
return decorated
@datetime_decorator
def main_function_1():
print "MAIN FUNCTION 1 START"
@datetime_decorator
def main_function_2():
print "MAIN FUNCTION 2 START"
@datetime_decorator
def main_function_3():
print "MAIN FUNCTION 3 START"
..... X 100번
출처: https://bluese05.tistory.com/30
import datetime
class DatetimeDecorator:
def __init__(self, f):
self.func = f
def __call__(self, *args, **kwargs):
print datetime.datetime.now()
self.func(*args, **kwargs)
print datetime.datetime.now()
class MainClass:
@DatetimeDecorator
def main_function_1():
print "MAIN FUNCTION 1 START"
@DatetimeDecorator
def main_function_2():
print "MAIN FUNCTION 2 START"
@DatetimeDecorator
def main_function_3():
print "MAIN FUNCTION 3 START"
my = MainClass()
my.main_function_1()
my.main_function_2()
my.main_function_3()
출처: https://bluese05.tistory.com/30
def a_decorator(func):
def wrapper(*args, **kwargs):
"""A wrapper function"""
# Extend some capabilities of func
func()
return wrapper
@a_decorator
def first_function():
"""This is docstring for first function"""
print("first function")
@a_decorator
def second_function(a):
"""This is docstring for second function"""
print("second function")
print(first_function.__name__)
print(first_function.__doc__)
print(second_function.__name__)
print(second_function.__doc__)
# output:
wrapper
A wrapper function
wrapper
A wrapper function
print("First Function")
help(first_function)
print("\nSecond Function")
help(second_function)
# output:
First Function
Help on function wrapper in module __main__:
wrapper(*args, **kwargs)
A wrapper function
Second Function
Help on function wrapper in module __main__:
wrapper(*args, **kwargs)
A wrapper function
출처:https://www.geeksforgeeks.org/python-functools-wraps-function/
-Demystifying @decorators in Python
https://sumit-ghosh.com/articles/demystifying-decorators-python/
고차 함수 호출
https://docs.python.org/2/library/functools.html#functools.wraps
반응형
'STUDY > Python' 카테고리의 다른 글
Django | 숙제 | 조건문&반복문&class (0) | 2022.05.27 |
---|---|
VSC 로 Django SQLite 설치 | conda | MAC (0) | 2022.05.27 |
파이썬 기초 👩🏻💻 점프 투 파이썬 📚 (0) | 2022.05.14 |
머신러닝 2주 차 | 이진 논리 회귀 , 다항 논리 회귀, 전처리 (0) | 2022.05.14 |
라이브러리? API? 패키지? 🧐| 머신러닝 1주 차 강의에서 사용한 라이브러리와 API 링크 (0) | 2022.05.14 |