[python] @ Decorator

지윤곽·2021년 1월 16일
0

python

목록 보기
2/2
  • 데코레이터 함수 : 중첩된 함수를 사용해야 할 경우, 훨씬 더 깔끔하게 코드를 짤 수 있음.
def hello_decorator(func): #함수를 파라미터로 받음
  def decorated():
    print('a')
    func()  #함수를 중심으로 앞과 뒤에 실행할 코드를 넣는다
    print('c')
  return decorated

@hello_decorator #데코레이터를 호출한다
def hello():
  print('b')
hello()
#output : a b c (hello_decorator(hello()) - 중첩된 함수가 출력됨)
profile
아는게 힘이다

0개의 댓글