[파이썬 Python] 데코레이터 @decorator

2020. 12. 17. 02:48python/초급 파이썬

728x90
반응형

 

데코레이터


쉽게말하면 함수로 함수를 꾸며주는 것이다.

여러함수에 일일히 추가해주는 것은 귀찬으므로 함수에 함수를 더해주는 것이다.

 

예를들면

def print_hello():
	print('hello')
    
#앞뒤에 print('*****') 을붙여주고싶을때

*****
hello
*****

def add_to_print_hello(original):
	def wrapper:
   	print('*****')
        original()
        print('*****')
    return wrapper

add_to_print_hello(print_hello)()



    	

@함수명으로 깔끔하게 써줄수 있다.

함수 앞뒤에 *****를 출력하고 싶은 함수위에 @add_to_print_hello 써주면 된다.

@add_to_print_hello
def print_hello():
	print('hello')

 

반응형