> ## Content Index
> Fetch the complete content index at: https://bongholee.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Python Decorator(데코레이터)
- URL: https://bongholee.com/python-decorator-dekoreiteo/
- Published: 2022-04-22T14:37:06.000Z
- Updated: 2022-05-08T11:57:43.000Z
- Author: Bongho Lee
- Tags: Python, Decorator, #Import 2026-11-22 06:32

## **왜 데코레이터를 사용할까?**

- 데코레이터는 함수를 수정하지 않은 상태에서 추가 기능을 구현할 때 사용

## **As-Is**

```
def hello():
    print('hello 함수 시작')
    print('hello')
    print('hello 함수 끝')
 
def world():
    print('world 함수 시작')
    print('world')
    print('world 함수 끝')
 
hello()
world()
```

## **To-Be**

```
def trace(func):                             # 호출할 함수를 매개변수로 받음
    def wrapper():
        print(func.__name__, '함수 시작')    # __name__으로 함수 이름 출력
        func()                               # 매개변수로 받은 함수를 호출
        print(func.__name__, '함수 끝')
    return wrapper                           # wrapper 함수 반환
 
@trace    # @데코레이터
def hello():
    print('hello')
 
@trace    # @데코레이터
def world():
    print('world')
 
hello()    # 함수를 그대로 호출
world()    # 함수를 그대로 호출
```

## **Comment**

- 함수를 수정하지 않은 상태에서 추가 기능을 구현할 때 사용한다고 하는데, 아직 크게 감이 오지는 않는다.
- 다만 실행 과정에서 로깅(Logging), 또는 디버깅(Debugging)할 때나,
- 또는 어댑터(Adapter)나 [@abstractmethod](https://dojang.io/mod/page/view.php?id=2389&ref=bongholee.com) 와 함께, 추상클래스 구현시 필요할 듯하다. 예를 들어 시스템 트레이딩을 구축하는데 업비트와, 빗썸에 대해서 공통적인 기능은 추상클래스를 통해서 메소드 목록만 우선 구축하고, 각각 구현은 상속 받아서 하면 될텐데 이럴 때 사용되는 듯.

## **Reference**

- [데코레이터 만들기](https://dojang.io/mod/page/view.php?id=2427&ref=bongholee.com)