본 포스팅은 나의 파이썬 기초를 탄탄히 다지기 위하여 작성한다. 따라서 여느 파이썬 기본 교재의 목차와 비교해보았을 때 뒤죽박죽일 수 있음을 감안해주길 바란다.
test_list = [1,2,3,4,5]
test_tuple = (1,2,3,4,5)
test_tuple[0] = 6
print(test_tuple)
Error!
파이썬은 메모리를 관리하기위해 숨겨진 힙 스페이스(heap space)를 사용한다. 모든 객체와 자료 구조들이 이 곳에 저장된다. 인터프리터가 스페이스를 관리하기 때문에 심지어 프로그래머 조차도 이 공간에 대해 접근하지 못한다. 더 나아가, 파이썬은 사용되지 않은 메모리를 재활용하고 메모리를 지워 힙 스페이스에서 사용 가능하게 하는 빌트인 가비지 컬렉터(garbage collector)를 소유하고 있다.
# Why we need decorator?
def func1():
print("func1")
def func2():
print("func2")
def func3():
print("func3")
def funcN():
print("funcN")
print(func1())
print(func2())
print(func3())
print(funcN())
func1
func2
func3
funcN
간단한 함수다. 우리는 각각의 함수에 새로운 기능 추가를 원한다.
예를 들어, func1에 현재 시각을 출력 기능을 5번 추가하자.
import datetime
# Why we need decorator?
def func1():
print(datetime.datetime.now())
print(datetime.datetime.now())
print(datetime.datetime.now())
print(datetime.datetime.now())
print(datetime.datetime.now())
print("func1")
def func2():
print("func2")
def func3():
print("func3")
def funcN():
print("funcN")
그러나 갑자기 다른 모든 함수들에 대해서도 동일한 기능을 추가하고 싶어졌다. 그러면 어떻게 해야할까? 직관적으로 아래처럼 할 것이다.
import datetime
# Why we need decorator?
def func1():
print(datetime.datetime.now())
print(datetime.datetime.now())
print(datetime.datetime.now())
print(datetime.datetime.now())
print(datetime.datetime.now())
print("func1")
def func2():
print(datetime.datetime.now())
print(datetime.datetime.now())
print(datetime.datetime.now())
print(datetime.datetime.now())
print(datetime.datetime.now())
print("func2")
def func3():
print(datetime.datetime.now())
print(datetime.datetime.now())
print(datetime.datetime.now())
print(datetime.datetime.now())
print(datetime.datetime.now())
print("func3")
def funcN():
print(datetime.datetime.now())
print(datetime.datetime.now())
print(datetime.datetime.now())
print(datetime.datetime.now())
print(datetime.datetime.now())
print("funcN")
만약 [N=100]이라고 한다면 우리는 100번의 노가다 작업을 해야할 것이다. 너무 끔찍하다 ...! 😱
이와 같은 불상사를 방지하기 위하여 등장한 것이 바로 [데코레이터] 기능이다!
데코레이터 기능을 사용한다면 아래와 같이 간편하게 코딩을 할 수 있다.
import datetime
# Why we need decorator?
def datetime_deco(func):
def decorated():
print(datetime.datetime.now())
print(datetime.datetime.now())
print(datetime.datetime.now())
print(datetime.datetime.now())
print(datetime.datetime.now())
func()
return decorated
@datetime_deco
def func1():
print("func1")
@datetime_deco
def func2():
print("func2")
@datetime_deco
def func3():
print("func3")
@datetime_deco
def funcN():
print("funcN")
func1()
func2()
func3()
funcN()
2022-11-30 21:45:42.942180
2022-11-30 21:45:42.942204
2022-11-30 21:45:42.942209
2022-11-30 21:45:42.942212
2022-11-30 21:45:42.942214
func1
2022-11-30 21:45:42.942220
2022-11-30 21:45:42.942222
2022-11-30 21:45:42.942225
2022-11-30 21:45:42.942228
2022-11-30 21:45:42.942230
func2
2022-11-30 21:45:42.942235
2022-11-30 21:45:42.942237
2022-11-30 21:45:42.942240
2022-11-30 21:45:42.942242
2022-11-30 21:45:42.942245
func3
2022-11-30 21:45:42.942250
2022-11-30 21:45:42.942252
2022-11-30 21:45:42.942255
2022-11-30 21:45:42.942257
2022-11-30 21:45:42.942260
funcN