decorator: 함수에 공통, dispatch: 오버로딩

markyang92·2022년 3월 20일
0

python

목록 보기
17/43
post-thumbnail

decorator

  • '공통 함수' 작성 -> 코드 재사용성
    • none_doce(5)를 해야 진짜 실행된다. (불편.. 그래서 decorator 실행됨)

  • decorator

예제 1. 함수'들' 중에 다 공통으로 들어가는 함수가 있다.

def smile():
	print('^^')
   
def angry():
	print('ㅡㅡ^')

def love():
	print('love')
  • 여기서 갑자기 각 함수에 copyright를 출력하는 걸 추가해야한다고 한다.
    • 모든 함수에 다 넣어야하나?
      • decorate를 사용하자!


dispatch(오버로딩)

from multipledispatch import dispatch

@dispatch(list)
def gen(arg_list: list):
    print(arg_list)

@dispatch(str)
def gen(string: str):
    print(string)


if __name__ == '__main__':
    gen([1,2,3,4,5])
    gen('hello')

profile
pllpokko@alumni.kaist.ac.kr

0개의 댓글