파이썬을 공부하면서 이렇게 많이 뒤에 온적이 없었기 때문에, 데코레이터는 처음 공부해는 부분이다. 그래서 더 어렵고 낯설다.... ㅠㅠㅠ
class Calculator:
@staticmethod
def add(x,y):
print(x+y)
저기에 있는 @staticmethod가 데코레이터이다.
데코레이터 만든 예를 보자.
def trace(func):
def wrapper():
print(func.__name__, '함수 시작')
func()
print(func.__name__, '함수 끝')
return wrapper
def hello():
print('hello')
def world():
print('world')
여기서
trace_hello = trace(hello)를 하면 hello 함수를 wrapper에 담은 다음 그걸 trace_hello에 넣어준다.
trace_hello()하면 그 넣어준 래퍼를 실행해줌으로써 실행이 되게된다.