반응형
Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

CDW (Coding Discussion World)

python 기초 - decorator 본문

Python/Study

python 기초 - decorator

pymin 2022. 12. 24. 17:48
반응형

 

파이썬의 decorator안 데코라는 뜻 처럼 기존 함수를 수정하지 않고 데코레이션을 통해 함수를 수정하는 것이다.

우선 기존의 밋밋한 함수를 보자

def print_test():
    print("deco test")    

print_test()
deco test

 

print_test란 함수를 실행시켰는데 해당 무구만 출력되니 뭔가 단조로운 느낌이다. decorator를 사용해서 출력되는 문구의 위 아래에 다른 것들이 출력되도록 하자.

def decorator(deco):
    def deco_print():
        print("="*50)
        deco()
        print("="*50)
    return deco_print

@decorator
def print_test():
    print("deco test")    

print_test()
==================================================
deco test
==================================================

 

이렇게 하면 기존 함수를 수정하지 않고 데코(?)가 가능해진다.

 

 

반응형